Reputation:
Good Morning!, I have a code in VB.Net that transfer data from database into form controls like textbox and combobox. It works perfectly except for one. It doesnt display the data in the combobox.
Dim con As MySqlConnection = New MySqlConnection("server=192.168.2.246;userid=root;password=admin1950;database=inventory")
Dim cmd As MySqlCommand = New MySqlCommand("select RIDate,DateReceived,Status,PreparedBy,Location,Supplier,TotalAmount,GeneralRemarks,DocNo,CRFNo,RecBy,DATE_FORMAT(PrintDate,'%m/%d/%Y')as PrintDate from receiving where RINo = '" & TextBox1.Text & "'", con)
Dim reader As MySqlDataReader
con.Open()
reader = cmd.ExecuteReader
While reader.Read
DateTimePicker1.Value = reader.GetString("RIDate")
DateTimePicker2.Value = reader.GetString("DateReceived")
TextBox2.Text = reader.GetString("Status")
TextBox3.Text = reader.GetString("PreparedBy")
ComboBox1.Text = reader.GetString("Location") <----------This
ComboBox2.Text = reader.GetString("Supplier") <----------is the Problem
Try
TextBox4.Text = reader.GetString("PrintDate")
Catch
TextBox4.Text = ""
End Try
TextBox6.Text = reader.GetString("TotalAmount")
Try
TextBox7.Text = reader.GetString("GeneralRemarks")
Catch ex As Exception
TextBox7.Text = ""
End Try
TextBox11.Text = reader.GetString("DocNo")
Try
TextBox9.Text = reader.GetString("CRFNo")
Catch
TextBox9.Text = ""
End Try
Try
TextBox10.Text = reader.GetString("RecBy")
Catch
TextBox10.Text = ""
End Try
End While
con.Close()
Its working but when I change the combobox into Dropdownlist the data doesnt show any more. what do you think is the prob?
TYSM for help
Upvotes: 0
Views: 133
Reputation: 369
If you're trying to set the ComboBox
from a single record, instead of using the text property, you could use the FindString property of the ComboBox
to set it.
But if there are no matching items found in the ComboBox
, it will return -1, which will cause an IndexOutOfRange
exception, so be careful. So you might want to do something else if it returns -1, you'll have to handle that case differently.
Example:
Dim index As Integer = ComboBox1.FindString(reader.GetString("Location"))
If (index <> -1) Then
ComboBox1.SelectedIndex = index
Else
'-- Do something else here
End If
Upvotes: 0
Reputation: 4506
The DropDownList
style ensures that users cannot select an element that isn't in the list. Make sure the value of reader.GetString("Location")
is in ComboBox1
's data source.
If the datasource is a list of strings then this should work fine. If it's a list of objects where you are binding using DisplayMember
and ValueMember
then the .Text
property will match against the DisplayMember
field.
For instance, if you have an object with {.Name = "New York City", .Value = "NYC"}
and the ComboBox has DisplayMember = "Name"
ValueMember = "Value"
. Then .Text
needs to be New York City
to match. If you need to match on NYC
instead set .SelectedValue = "NYC"
Upvotes: 1