DataReader,ArrayList, ListBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm developing in vs 2005 beta2 now. I've been using a table adapter to
populate comboboxes. That's fine, because these are mostly lookup tables
with 10 to 30 rows. But then I came to Authors. I have hundreds of authors.

I started a thread in the vs 2005 forums looking for a more economical way.
It boiled down to the topic title, but nobody to find a way to do it.
ArrayLists have one dimension. I need two entries, one for display, the
other for value. There is no databinding itself for the comboboxes. I use a
tableadapter insertquery to add a row. As you know, in 2005 we avoid all
those parameter statements.

I've looked through my six vs studio 2003 books - framework corereference,
vb.net core reference, ado.net core reference and three others. I've queries
msdn backward and forward. The datareader examples are message boxes or
filling textboxes. Never an example showing a real use for them, like
filling the display and value members of a combobox or listbox.

Can anybody help me here?

dennist685
 
dennist685,

One option is to retrieve just the data you need into a datatable. Then bind
the datatable to a listbox, combobox, etc. Once the control is bound, the
datatable is no longer needed. For example:

Dim cn As New OleDb.OleDbConnection("Provider=SQLOLEDB;Initial
Catalog=CustomerDataSQL;User ID=xx;Password=abcde")
Dim cmd As New OleDb.OleDbCommand
Dim dAdapter As New OleDb.OleDbDataAdapter
Dim dTable As New DataTable

cmd.CommandText = "Select LastName + ', ' + FirstName As
CustomerName, ID From Customers Order By CustomerName"

cn.Open()
cmd.Connection = cn

dAdapter.SelectCommand = cmd
dAdapter.Fill(dTable)

cn.Close()

lstCustomers.DisplayMember = "CustomerName"
lstCustomers.ValueMember = "ID"
lstCustomers.DataSource = dTable

The dTable data table was just a temporary variable, used to fill the control.

Now when the user selects a name from the combobox, the ID can be retrieved
from the combobox and used as a primary key to get just the record you are
interested in.

Kerry Moorman
 
Back
Top