473,782 Members | 2,393 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("e xec populateCities 'North America'",conne ct)
dtReader = cmdSelect.Execu teReader()

depCity1.Dataso urce = dtReader
depCity1.Datate xtField = "city_name"
depCity1.DataBi nd()

destCity2.Datas ource = dtReader
destCity2.Datat extField = "city_name"
destCity2.DataB ind()
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.DataTe xtField = 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 3188
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.ExecuteRead er()
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.I tems.Add(item)
DropDownList2.I tems.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**********@p lanet.nl> дÈëÏûÏ¢
news:u8******** ******@TK2MSFTN GP09.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.ExecuteRead er()
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.I tems.Add(item)
DropDownList2.I tems.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**********@p lanet.nl> дÈëÏûÏ¢
news:#Q******** ******@TK2MSFTN GP12.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("e xec getCustomers",c onnect)
dtReader = cmdSelect.Execu teReader()

Name.Datasource = dtReader
Name.DatatextFi eld = "name"
Name.DataBind()

Address.Datasou rce = dtReader
Address.Datatex tField = "address"
Address.DataBin d()

City.Datasource = dtReader
City.DatatextFi eld = "city"
City.DataBind()

State.Datasourc e = dtReader
State.DatatextF ield = "state"
State.DataBind( )

Zip.Datasource = dtReader
Zip.DatatextFie ld = "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("e xec getCustomers",c onnect)
dtReader = cmdSelect.Execu teReader()

Name.Datasource = dtReader
Name.DatatextFi eld = "name"

Address.Datasou rce = dtReader
Address.Datatex tField = "address"

City.Datasource = dtReader
City.DatatextFi eld = "city"

State.Datasourc e = dtReader
State.DatatextF ield = "state"

Zip.Datasource = dtReader
Zip.DatatextFie ld = "zip"

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

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

Thanks,

Tom.

"Cor Ligthert" <no**********@p lanet.nl> дÈëÏûÏ¢
news:#Q******** ******@TK2MSFTN GP12.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. 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...
20
5535
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 "clean up" the DataView once it is not referenced and will not effect the number of connections to the database. A DataReader on the other hand always maintains a connection to the database and must be explicitly closed (Do not rely on garbage...
1
4095
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 the same code, I would like to verify if method 2 is the right way to reuse the same reader object. If this is the case could I open a reader object (that is global) at
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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 we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.