473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reusing DataReader

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
5 3162
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
467
by: Thomas Scheiderich | last post by:
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. ...
20
5509
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
1
4074
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.