listbox problem

vampke

New member
Joined
Feb 15, 2008
Messages
3
Programming Experience
Beginner
Hi guys,

This problem is killing me!
I'm trying to get items from a table in a listbox.
I use
Public Function GetItems(ByRef DisplayMember As String, ByRef ValueMember As String) As DataTable
DisplayMember = "name"
ValueMember = "id"

Sql = "SELECT id, name FROM items"

'8< snip code to connect to database and execute sql statement

DS.Tables.Add(DT)
Return DT
End Function
to get the items in my datatable

I call on this function with this code:
lstItems.DataSource = _clsItems.GetItems(lstItems.DisplayMember, lstItems.ValueMember)

However, when I try to select an item in my listbox, I get an error saying that "conversion from String "id" to type integer is not valid. Apparently the integer is not passed to the listbox valuemember, in stead a string value is set as valuemember.

My database is very simple:
id->int(10) not null, auto_increment, primary key
name -> varchar(45) not null
If I try putting this in a datagridview, I get name and id as it is meant to.
Why is this not working in the listbox?
 
I think you'll find that that error has nothing whatsoever to do with that code. I'm guessing that you're handling the SelectedIndexChanged event and then you're assigning the ValueMember property to an Integer variable, right? Of course that can't work because the ValueMember property is the name of a column, not a value from that column. If you want the value from that column for the selected item then you want the SelectedValue property.
 
Back
Top