filling an array from a stored proc using a datareader

G

Guest

Hi, I was wondering if anyone can help me with a problem. I am using a datareader to fill my combobox (Combobox3) based on a stored proc
This code is in the SelecetedIndexChanged event of combobox2. i.e when combobox2 is clicked the code below is run to fill combobox3.
What i need help doin is gettin the values that are retireved to also fill an arraylist that i have
I.e the values that the dataredaer retrieves i want to be placed into an array as well as the combobox. I have filled the combobox but do not know how to fill the array. Can anyone help me? I have tried my version of it below but this only shows one item in the array.

Tr
Cn.Open(
Dim reader3 As SqlDataReade
reader3 = cmd.ExecuteReader(
While reader3.Rea
Combobox3.Items.Add(reader3.GetSqlString(0)

'place an array here of all the caravan invs availbl
AvailCaraInv.Add(reader3.GetSqlString(0)
DataGrid1.DataSource = AvailCaraIn

End Whil
Catch ex As Exceptio
MsgBox(ex.Message
Finall
Cn.Close(
End Tr
 
W

William Ryan eMVP

Bhavna:

A few misc comments...

First, the code looks fine assuming that's an arraylist. If the combobox is
getting filled then the ArrayList should to.

Just to be sure, After the read loop before the proc exits, add this code:
Debug.Assert(ComboBox3.Items.Count <> AvailCaraInv.Count)

Since I don't see a clear statement for the combo (you may have it outside
of the try catch but I'm guessing you may still have values in there and
that's causing the mismatch) let's just verify that the Combo is getting
filled but the arraylist isn't. Since those are right after each other and
using the same syntax, it's hard to believe that's the cause.

Next, firing another query each time the ComboBoxes index changes is
probably wasteful. Remember that ADO.NET allows you to cache everything
locally so I would advise using two datatables and then using a datarelation
http://www.knowdotnet.com/articles/datarelation.html . Then, the
DataRelation will take care of populating everything for you. You could
also use a DataView and reset the .RowFilter each time... This will give you
better performance and take stress off of your database. I know this might
sound trite, but people may erroneously click around on a combobox and do
you really want to make a whole trip to the db each time? If so , you
increase network traffic, stress on the db, and you create an Extra
Connection/Command and DataReader object. I'd really recommend using a
DataBound approach.

However, that's not what you were asking so before I get ahead of myself.
Add the line of code above and tell me if a Big Ugly Assertion box pops up.
If not, then the number of items in the combobox match the array list and
something else is making it look like it's not.

Also, make sure if you are using this approach that you clear the combobox
each time b/c if you don't, the old values will stay there and it may not
be readily evident.

Let me know about the assertion and we'll take it from there.

Cheers,

Bill
Bhavna said:
Hi, I was wondering if anyone can help me with a problem. I am using a
datareader to fill my combobox (Combobox3) based on a stored proc.
This code is in the SelecetedIndexChanged event of combobox2. i.e when
combobox2 is clicked the code below is run to fill combobox3.
What i need help doin is gettin the values that are retireved to also fill an arraylist that i have.
I.e the values that the dataredaer retrieves i want to be placed into an
array as well as the combobox. I have filled the combobox but do not know
how to fill the array. Can anyone help me? I have tried my version of it
below but this only shows one item in the array.
 

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

Top