URGENT:Viewing MULTIPLE RECORDS from database using DATA READER

G

Guest

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!
 
S

Sahil Malik

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
 
G

Guest

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!
 
G

Guest

Hi Sahil,

I am kind of new to ASP.NET & C#. Can you please explain in detail how to
use solution 1 for viewing multiple records from database, i.e you told me
to::

"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."

How to go about doing this? Any help is appreciated coz 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!
 
S

Sahil Malik

pmud,

I'd start looking at any standard DataBinding example.

A good indepth explanation can be found at -
http://www.developerfusion.com/show/4410/1/
or a shorter more concise explanation can be found at -
http://www.dotnetjunkies.com/quickstart/aspplus/doc/webdatabinding.aspx

But let me give you a headstart of "What databinding is".

Basically it's pretty simple, you have your data that you wish to represent
in a standard object that implements IList, Arraylist or Datasets are good
examples of two wellknown objects that implement IList.
Any control that has the ability to be databound to such an object, takes
advantage of the presence of IList and calls relevant methods to do a lot of
work for you i.e. it will associate the various controls and "bind" them to
various values in the object that contained your data.

So what you want to do is, you wish to get all the data in a databindable
format - arraylist or dataset, and basically "DataBind" it to your ASP.NET
form. .. How to do DataBinding is very nicely explained in the two article
links I just gave you.

Another good link for afterwards is -
http://www.asp.net/Tutorials/quickstart.aspx

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


pmud said:
Hi Sahil,

I am kind of new to ASP.NET & C#. Can you please explain in detail how to
use solution 1 for viewing multiple records from database, i.e you told me
to::

"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."

How to go about doing this? Any help is appreciated coz 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!
 
G

Guest

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!
 
W

W.G. Ryan eMVP

I agree with Sahil - I'd lean away from a DataReader.

One big problem with a Reader is that in order for it to be of any use, you
need an open and available connection. You don't know how long someone is
going to look at a given record, so even if you could accomplish this in a
straightforward manner - it won't scale well. Moreoever you can't serialize
a datareader so you'd need to fire another one between post backs - double
yuck.



--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top