By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Reusing DataReader

P: n/a
I have 2 dropdowns that are exactly the same and I don't want to re-read
for each.

Is there a way to do something like below where I read the data, bind to
depCity1 and then bind to destCity2. I find that the second one always
is blank. I assume this is because the DataReader is read-forward only.

************************************************** ******
connect.open()
cmdSelect = New OleDbCommand("exec populateCities 'North America'",connect)
dtReader = cmdSelect.ExecuteReader()

depCity1.Datasource = dtReader
depCity1.DatatextField = "city_name"
depCity1.DataBind()

destCity2.Datasource = dtReader
destCity2.DatatextField = "city_name"
destCity2.DataBind()
connect.close()
********************************************

If I can't reuse the dtReader I already have, how do I quickly make
dest2City2 = depCity1?

Also, I have a stored procedure that doesn't pass back a title. Is
there a way to do the above where I do something like:

depCity1.DataTextField = dtReader(0).

I know the above isn't correct, just trying to find a way to say look at
column 0.

Thanks,

Tom.

Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Thomas,

Almost the same question was in the Adonet newsgroup yesterday.
The answer where the same, use the databinding, however I made a code sample
for you how you can do it using the datareader.

I hope this helps?

Cor
\\\
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader()
While rdr.Read()
Dim item As New ListItem
item.Text = rdr.GetInt32(0).ToString 'for an integer value from the
database
item.Value = rdr.GetString(1) 'for a string value from the database
DropDownList1.Items.Add(item)
DropDownList2.Items.Add(item)
End While
///
I hope this helps?

Cor
Nov 18 '05 #2

P: n/a
i think you can store the data of dataReader into a IList container,then you
can user the IList anywhere
"Cor Ligthert" <no**********@planet.nl> 写入消息
news:u8**************@TK2MSFTNGP09.phx.gbl...
Hi Thomas,

Almost the same question was in the Adonet newsgroup yesterday.
The answer where the same, use the databinding, however I made a code sample for you how you can do it using the datareader.

I hope this helps?

Cor
\\\
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader()
While rdr.Read()
Dim item As New ListItem
item.Text = rdr.GetInt32(0).ToString 'for an integer value from the
database
item.Value = rdr.GetString(1) 'for a string value from the database
DropDownList1.Items.Add(item)
DropDownList2.Items.Add(item)
End While
///
I hope this helps?

Cor

Nov 18 '05 #3

P: n/a
Hi Coollzh,

I said this question has been in the adonet group yesterday too, there the
answer was to use an adapter and a datatable, however this is the shortest
way to do it in my opinion with the datareader (for which I see no real
advantage).

However what is the advantages of using an Ilist solution, I did think of
giving that, however I saw no advantage.

Cor
Nov 18 '05 #4

P: n/a
Actually , in most conditions the IList filled with DataReader is more
effecitive solution.
IList is a interface, you can bind the object that implement IList interface
to a datagrid or a Repeator.

"Cor Ligthert" <no**********@planet.nl> 写入消息
news:#Q**************@TK2MSFTNGP12.phx.gbl...
Hi Coollzh,

I said this question has been in the adonet group yesterday too, there the
answer was to use an adapter and a datatable, however this is the shortest
way to do it in my opinion with the datareader (for which I see no real
advantage).

However what is the advantages of using an Ilist solution, I did think of
giving that, however I saw no advantage.

Cor

Nov 18 '05 #5

P: n/a
coollzh wrote:
Actually , in most conditions the IList filled with DataReader is more
effecitive solution.
IList is a interface, you can bind the object that implement IList interface to a datagrid or a Repeator.

Do you have an example on how you can do that? I am trying to play with
all the options to learn the pluses and minuses of each technique.

I was looking at other ways other than looping through and adding each
field one by one.

The way I originally showed was good for only binding to one object (I
just had need of using the same data in a couple of places and couldn't
figure out why I couldn't just bind to another object. Later I found
out why - read forward only with DataReader).

I then thought there might be a way to do some type of doing an
assignment without looping through the object (object1.values =
object2.values).

I read that the DataReader was the fastest and most efficient method for
reading, although not as robust as the DataSet (adapter). For the Web,
according to what I read (and I may be wrong here), you want speed and
the use of DataReader with Stored Procedures would provide the maximum
data access speed.

But speed is not the only factor to consider, in my case - using
DataReader, I would have to re-read multiple times to get the same
information to each object (or loop through assigning to each object
together as was demonstrated by Cor).

This then gives me another problem. What if I am passed back multiple
fields in my DataReader (name, address, city, state and zip, for
example) and want to do the databinding I did in my first post - I
assume this would not be possible. For example:

************************************************** ******
connect.open()
cmdSelect = New OleDbCommand("exec getCustomers",connect)
dtReader = cmdSelect.ExecuteReader()

Name.Datasource = dtReader
Name.DatatextField = "name"
Name.DataBind()

Address.Datasource = dtReader
Address.DatatextField = "address"
Address.DataBind()

City.Datasource = dtReader
City.DatatextField = "city"
City.DataBind()

State.Datasource = dtReader
State.DatatextField = "state"
State.DataBind()

Zip.Datasource = dtReader
Zip.DatatextField = "zip"
Zip.DataBind()
connect.close()
********************************************

If this is the case, would this work where I just use a Page DataBind:
************************************************** ******
connect.open()
cmdSelect = New OleDbCommand("exec getCustomers",connect)
dtReader = cmdSelect.ExecuteReader()

Name.Datasource = dtReader
Name.DatatextField = "name"

Address.Datasource = dtReader
Address.DatatextField = "address"

City.Datasource = dtReader
City.DatatextField = "city"

State.Datasource = dtReader
State.DatatextField = "state"

Zip.Datasource = dtReader
Zip.DatatextField = "zip"

DataBind()
connect.close()
********************************************

Also, what was the title of the posts that dealt with this yesterday

Thanks,

Tom.

"Cor Ligthert" <no**********@planet.nl> 写入消息
news:#Q**************@TK2MSFTNGP12.phx.gbl...
Hi Coollzh,

I said this question has been in the adonet group yesterday too, there the
answer was to use an adapter and a datatable, however this is the shortest
way to do it in my opinion with the datareader (for which I see no real
advantage).

However what is the advantages of using an Ilist solution, I did think of
giving that, however I saw no advantage.

Cor



Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.