1
votes

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ListBox1.Items.Clear()

    sql = "SELECT * FROM testing_mysql_vb"
    Try
        dbcomm = New MySqlCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader()

        While dbread.Read
            ListBox1.Items.Add(dbread("product_name")("product_quantity"))
        End While

        dbread.Close()
    Catch ex As Exception
        MsgBox("Error in collecting data from Database. Error is :" & ex.Message)
        dbread.Close()
        Exit Sub
    End Try
End Sub
End Class

i can't get the data from my database

it says error in conversion from string to integer

3

3 Answers

1
votes

You are passing dbread("product_name")("product_quantity") to ListBox.Items.Add. That doesn't work. Maybe you want to combine both columns:

Dim prodNameVal As Object = dbread("product_name")
Dim productQuantityValue As Object = dbread("product_quantity")
ListBox1.Items.Add(String.Format("{0}: {1}", prodNameVal, productQuantityValue))
1
votes

If your DB is giving you an integer as return value and you need to place it in a place (a controle or response.write) that only accepts strings, you can use. toString()

0
votes

I assume that the product quantity is an integer and you are needing a string, and if so, you can fix it like this.

Change

ListBox1.Items.Add(dbread("product_name")("product_quantity"))

TO

   ListBox1.Items.Add(dbread("product_name") & ("product_quantity").tostring())

OR

 ListBox1.Items.Add(dbread("product_name") & Cstr(("product_quantity")))