Re: Using the DataTextField and DataValue Field in a ListBox

Tech-Archive recommends: Fix windows errors by optimizing your registry



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)))

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


"l7alabeh" <l7alabeh@xxxxxxxxx> wrote in message
news:1158744418.462352.18180@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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



.