Using the DataTextField and DataValue Field in a ListBox

  • Thread starter Thread starter l7alabeh
  • Start date Start date
L

l7alabeh

Hi,

I am trying to take from a database 3 values, concatenate the first two
of them and insert them in a ListBox (in the DataTextField) and the 3rd
value in the DataValueField.

The problem is that I am able only to add the first two values
concatenated (using the DataReader) but the DataValueField is not
taking any new value, it is taking the value of the text.

Can you please help me to solve this problem?
 
What do you think your car mechanic will answer you if you call him
complaying that the car has a problem?

Post your code.
 
This is the code:

Sub ListBoxBind()

connstrconf.ConnectionString = strcnnconf

Dim NameDataReader As OleDbDataReader
Dim NameOleDbCommand As OleDbCommand


NameOleDbCommand = New OleDbCommand("select first_name,
last_name, dbid from cfg_person", connstrconf)

connstrconf.Open()
NameDataReader = NameOleDbCommand.ExecuteReader()


Do While (NameDataReader.Read())
ListBox1.Items.Add(NameDataReader.GetValue(0) & " " &
NameDataReader.GetValue(1))
ListBox1.datavaluefield = NameDataReader.GetValue(2)
Loop

NameDataReader.Close()
connstrconf.Close()
End Sub
 
datavaluefield is supposed to be set to the name of the field. Your code
assigns to it the value of the field. And anyway, datavaluefield is used
only in databinding and you are not using any databinding, you are adding
items programatically.

You should do something like

ListBox1.Items.Add(New ListItem (NameDataReader.GetValue(0) & " " &
NameDataReader.GetValue(1), NameDataReader.GetValue(2)))
 
Back
Top