Reusing DataReader

T

Thomas Scheiderich

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.
 
C

Cor Ligthert

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
 
C

coollzh

i think you can store the data of dataReader into a IList container,then you
can user the IList anywhere
 
C

Cor Ligthert

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
 
C

coollzh

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.
 
T

Thomas Scheiderich

coollzh said:
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.
 
W

Welman Jordan

Hello Thomas,

It seems that your latter approach would not work.
Databinding can not be performed simultaneously, I guess.

Since the databinding of the IDataReader, will Read() all
records from the forward only reader, thus, one control,
for example, the Name control, finishes the databinding,
the second control, for example, the Address control,
would not have any record to Read() any more.

W. Jordan
 
C

Cor Ligthert

Hi Thomas,

For me there are basicly only 2 methods (assuming you want to use more
controls in one time)
- using the datareader and create a collection as the ilist however that
can also be a datatable
- using the dataadapter which uses the datareader internally and than the
datatable enclosed in a dataset and than the dataview to reorder or select
the data.

I prefer the last one, with which you can by aspnet easy persist the dataset
in a session to use them between pages.

I hope this helps?

Cor
 
T

Thomas Scheiderich

Cor said:
Hi Thomas,

For me there are basicly only 2 methods (assuming you want to use more
controls in one time)
- using the datareader and create a collection as the ilist however that
can also be a datatable
- using the dataadapter which uses the datareader internally and than the
datatable enclosed in a dataset and than the dataview to reorder or select
the data.


Actually, I found another way, although it entails doing multiple
selects but one trip to the server. I found this yesterday where you
can send multiple request in the same string separated by semicolons and
then use datareader.NextResult() to get the next response. The
drawback, as I mentioned is that it has to do multiple requests. But
since the request would be the same, I assume the data would be cached
(or at least the statement would be). I tried it and it works great.
In the following example, I am filling 3 dropdownboxes with the same set
of carriers and it does the job.

******************************************************************************
connect.open()
cmdSelect = New OleDbCommand( _
"exec populateCarriers;" & _
"exec populateCarriers;" & _
"exec populateCarriers",connect)
dtReader = cmdSelect.ExecuteReader()

with preferedAirline
preferedAirline.Datasource = dtReader
preferedAirline.DataTextField = "carrier"
preferedAirline.DataBind()
end with

dtReader.NextResult()

with preferedAirline2
preferedAirline2.Datasource = dtReader
preferedAirline2.DataTextField = "carrier"
preferedAirline2.DataBind()
end with

dtReader.NextResult()

with preferedAirline3
preferedAirline3.Datasource = dtReader
preferedAirline3.DataTextField = "carrier"
preferedAirline3.DataBind()
end with

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

Thank,

Tom.
 
C

Cor Ligthert

Hi Thomas,

See under the message.
Actually, I found another way, although it entails doing multiple
selects but one trip to the server. I found this yesterday where you
can send multiple request in the same string separated by semicolons and
then use datareader.NextResult() to get the next response. The
drawback, as I mentioned is that it has to do multiple requests. But
since the request would be the same, I assume the data would be cached
(or at least the statement would be). I tried it and it works great.
In the following example, I am filling 3 dropdownboxes with the same set
of carriers and it does the job.

This is for me the datareader method which I do not like because it is
limited, than my sample is much more effective I think.

(You only found a way to write it in one string however the result is the
same in my opinion).

You probably don't know how many alternatives there are using the datatable.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top