How is it that you are faced with such urgency when you evidently have not
yet learned the fundamentals? The first thing I would do, if I were you, is
address this concern with the right people.
Having said that, you need to study some MSDN articles, such as:
"Introduction to Data Access in Web Forms Pages", "Web Data Access Strategy
Recommendations", and "Data Binding Single-Value Web Server Controls at
Design Time". As questions arise from reading those, look up other things up
in MSDN.
You're going to need to create a dataset. To get started with that I would
suggest the Data Adapter Configuration Wizard in Visual Studio.
(View-->Toolbox-->Data-->OleDbDataAdapter - or SqlDataAdapter, for SQL
Server). Make sure you specify the parameter(s) that will contain the
value(s) the user enters for indicating what rows are to be displayed in your
web form. Then, use the "Generate Dataset" feature (right click on the
DataAdapter icon you just created) to generate a typed DataSet that will hold
the specific data columns required for your web form.
Bear in mind that the code that will be generated by the wizard may need
some tweaking as you proceed (especially if you app needs to do inserts,
updates, and deletes), but those needs should become evident in time.
Once you have a correctly configured DataAdapter and DataSet it only takes
one line of code to populate each table in the DataSet. Something like
this...
OleDbDataAdapter1.Fill(DataSet11, "TableName")
(If the DataSet has only one DataTable, you don't even need the TableName.)
You can use the Form Designer ("Properties" window) to bind your controls,
if they are on the same form as the DataSet. Either way, you need to execute
the DataBind method for each control, like this...
TextBox1.DataBind()
With bound controls, I think there's a way to go from one row to the next
with the DataView Maganer, or possibly the Enumerator that is exposed by a
table's Default View (dt.DefaultView.GetEnumerator).
On the other hand, it's probably just as easy to just keep an index value in
a persisted variable (probably in ViewState) to keep track of what the
current record during each round-trip. You could have "Previous" and "Next"
buttons that would decrement and increment that index. The index could serve
as a subscript to the DataTable...
TextBox1.text = DataSet11.Tables("TableName").rows(x)
....where "x" is the index. This would serve the same purpose.
You also need to consider state management, at least as far as your DataSet
is concerned. Keeping the DataSet in Session state is usually a good option
to get started - and will work fine most of the time, unless you have a
pretty large scale application.
I hope that will at least get you started - Good Luck
pmud said:
Hi Sahil,
I am kind of new to ASP.NET & C#. Can you please explain in detail how to go
with solution 1 for viewing multiple records from database.
The solution 1 you told me was::
Use Dataset/not datareader since that is databindable and you could use
binding to go thru the records one by one using record navigation controls.
Please help soon. Its urgent.
Thanks
Sahil Malik said:
A number of solutions exist for this problem.
a) Use Dataset/not datareader since that is databindable and you could use
binding to go thru the records one by one using record navigation controls.
b) Not good solution: Call DataReader.Read() on the button click, though
that's hella awful because you'd keep a d/b connection open all the while
until the web based user pages thru the records.
c) If you absolutely cannot use dataset, you could instead use datareader to
databind (per suggest #a ) as illustrated here (for a windows app, but the
same concept applies) -
http://dotnetjunkies.com/WebLog/sahilmalik/archive/2004/12/11/36078.aspx
In your code below, you are simply firehosing thru all the values one by one
and overwriting the previous value - hence presenting the user with the last
value - that's not what you need, you need to see the rows one by one on a
button press.
- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
http://blogs.apress.com/authors.php?author=Sahil Malik
pmud said:
Hi,
I have an ASP.NET application. I need to read records from the SQL database
based on a value entered by the user. I need to get the result in Text boxes.
I am using DATA READER for this.
But how can i view the next record from the database in the text boxes?? I
get one record in the text boxes by clicking a button....but how can i view
the next record???
My code is:
System.Data.SqlClient.SqlDataReader d1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
private void Button1_Click(object sender, System.EventArgs e)
{
sqlCommand1.Parameters["@a"].Value=txtC.Text;
sqlConnection1.Open();
d1= sqlCommand1.ExecuteReader();
while(d1.Read())
{
TextBox3.Text= d1["Company"].ToString();
TextBox4.Text=d1["Ceo"].ToString();
TextBox5.Text=d1["Month"].ToString();
}
d1.Close();
sqlConnection1.Close();
}
Please help. Its URGENT!