473,661 Members | 2,448 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 3181
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
5524
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
4084
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
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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
8754
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...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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
7362
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...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1740
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.