Bowi Prabono Bowi Prabono - 1 year ago 76 Question

VB.NET ComboBox SelectedIndex from MySQL

so I have done with show data form my database MySQL to ComboBox.. I'm using this code:

Private Sub Get_Product()

Dim command As New MySqlCommand
Dim reader As MySqlDataReader
Dim query As String = "SELECT * FROM product, writer, publisher WHERE product.writer = writer.writer AND product.publisher = publisher.publisher AND code = " & throwCode & " ORDER BY code"

Dim queryWriter As String = "SELECT DISTINCT writer, writer_name FROM writer ORDER BY writer_name"
Dim dataAdapter As New MySqlDataAdapter(queryWriter, conn)
Dim dataSet As New DataSet
dataAdapter.Fill(dataSet, "writer")

command = New MySqlCommand(query, conn)
reader = command.ExecuteReader

While reader.Read
TextBoxISBN.Text = reader("isbn")
TextBoxTitle.Text = reader("title")
TextBoxPage.Text = reader("page")

With (ComboBoxWriter)
.DataSource = dataSet.Tables("writer")
.DisplayMember = "writer_name"
.ValueMember = "writer"
.SelectedIndex = 0 **// Give atention to this code**
End With

TextBoxYear.Text = reader("year")
TextBoxCategory.Text = reader("category")
TextBoxCallNumber.Text = reader("call_number")
TextBoxWeight.Text = reader("weight")
TextBoxPurchasePrice.Text = reader("purchase_price")
TextBoxSellingPrice.Text = reader("selling_price")
TextBoxDiscount.Text = reader("discount")
TextBoxDescription.Text = reader("description")
TextBoxTag.Text = reader("tag")

TextBoxPusatPenerbit.Text = reader("pusat_penerbit")
TextBoxMrican.Text = reader("mrican")
TextBoxPaingan.Text = reader("paingan")

If (Not IsDBNull(reader("picture"))) Then
Dim byteImage() As Byte = reader("picture")
Dim tempImage As New System.IO.MemoryStream(byteImage)
PictureBoxPicture.Image = Image.FromStream(tempImage)
End If
End While

Catch ex As Exception
End Try
End Sub

In my code, I give some comment and bold to show you what I want to do.

So I have a writer code in product an writer code in writer (writer code named as writer), and I join the table to show writer name from writer code. But in ComboBox I want to show all of writer for editing but the default value is index of the writer that same like in product table.


enter image description here


enter image description here

If you see my first image that have a writer name Sutarjo Adisusilo but when I view it and want to use it as a default value in combobox that show A. Kardiyat Wiharyanto as indexvalue number 0..

I need help how to change the default value to be same like the picture number 1


Answer Source

I've updated this answer to an easier method. Double check the column names though.

ComboBoxWriter.SelectedIndex = ComboBoxWriter.FindStringExact(reader("writer_name"))