SqlDataReader --> Array --> combobox

R

RvGrah

In Vb.Net I used to use this code to put a few thousand numbers into an
array and then use the AddRange method to add them all at once to an
auto-complete combobox. It was hundreds of times faster than adding the
items directly to the combobox as I iterated through the SqlDataReader.
Since you can't redim Preserve in C# and you can't know the
datareader's count until after you close it, how would I do this in C#?


Dim jnumber(499) as Integer
Dim x as Integer

Dim drJobs As SqlDataReader = cmdAllNums.ExecuteReader
If drJobs.HasRows Then
Do While drJobs.Read
jnumber(x) = drJobs("JobName")
x += 1
If x Mod 500 = 0 Then ReDim Preserve jnumber(x + 499)
Loop
End If
drJobs.Close()

cbJobs.Items.AddRange(jnumber)
 
R

RvGrah

Correction: Dim jnumber(499) as Object, not integer is what worked in
vb.net

Thanx, Bob
 
R

RvGrah

Don't mean to keep answering my own posts, but would the best thing to
do be to forego the speed advantage of a forward-only sqlDataReader and
just fill a table instead? If I'm not mistaken you can grab the
table's row count as soon as you fill it and then create your array.

Thanx, Bob
 
J

Jon Skeet [C# MVP]

RvGrah said:
Don't mean to keep answering my own posts, but would the best thing to
do be to forego the speed advantage of a forward-only sqlDataReader and
just fill a table instead? If I'm not mistaken you can grab the
table's row count as soon as you fill it and then create your array.

Yes, using a DataTable would probably be preferable. DataReader is only
the way to go in a few situations.

If you really wanted to use DataReader, however, you could use an
ArrayList to store all the data as you went along, then call ToArray on
it to get an array out.
 
G

goody8

In Vb.Net I used to use this code to put a few thousand numbers into an
array and then use the AddRange method to add them all at once to an
auto-complete combobox. It was hundreds of times faster than adding the
items directly to the combobox as I iterated through the SqlDataReader.
Since you can't redim Preserve in C# and you can't know the
datareader's count until after you close it, how would I do this in C#?


Yikes! A "few thousand numbers" in a *combo box*? <shudder> Why on
earth would anyone do that?

But anyway, why don't you just bind the SqlDataReader to the combobox
directly, like this:

string sql = "SELECT Name, id FROM tblMyTable WHERE Name like 'F%' ";
SqlDataReader dr = conn.ExecuteReader(sql);
combobox.DataValueField = "id";
combobox.DataTextField = "Name";
combobox.DataSource = dr;
combobox.DataBind();
 
R

RvGrah

As the combobox is an auto-complete derivation of a standard combo, it
only takes users max of five key-strokes to get to the job number they
want to access. We don't want to bind the box to a datafield because
this makes it display the first item in the list rather than nothing
when they first open the form. Then they forget to change the value in
the combo and keep assigning things to the first item in the list (it's
been tried and it's a problem).

I like Jon's suggestion of the ToArray idea, I was unaware of the
ArayList having that method, I'll give it try!

Thanx all.
 

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