473,467 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Create 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 3158
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
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.