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
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))
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()
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")))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.