0

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

  • As a quick start to create a data application using windows forms designer features, you may find [this post](http://stackoverflow.com/a/37824444/3110834) useful. – Reza Aghaei Sep 24 '16 at 21:58

2 Answers2

1

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"

FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • Could you please also post the relevant code for setting up ComboBox1/2 so that we can give more advice. – FloatingKiwi Sep 24 '16 at 10:56
  • Sorry for the late reply, need to have a quick break but still im thinking this error and also yesterday is sunday. here in our countr. anyway seriously my code is all there and its working perfectly however there is a problem when im changing the comboboxstyle of the combobox. if combobox is dropdown then `.text = reader.getstring("value")` would be okay but when i change it to dropdownlist then no data shows –  Sep 26 '16 at 01:04
  • A combo box in `DropDownList` style will only show what is in the list of combo box items . You'll need to post the code that populates that combo box list with its items for us to help you. Your text above only shows how you intend to set the selected value. – FloatingKiwi Sep 26 '16 at 08:45
0

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
Froopy
  • 349
  • 4
  • 12