简体   繁体   English

从字符串转换为整数时出错

[英]error in conversion from string to integer

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ListBox1.Items.Clear() 私有子Button2_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理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 . 您正在将dbread("product_name")("product_quantity")传递给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. 如果您的数据库正在为您提供一个整数作为返回值,并且您需要将其放置在仅接受字符串的位置(controle或response.write),则可以使用。 toString() 的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")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM