【问题标题】:Error: Conversion from string "x" to type Integer is not valid. How do I turn an integer into a string?错误:从字符串“x”到整数类型的转换无效。如何将整数转换为字符串?
【发布时间】:2017-12-07 08:46:14
【问题描述】:

所以我正在制作一个程序,您可以在其中从组合框中选择产品,选择数量,它将返回列表框中的价格和数量以及总价。然后它将列表框中的数据输入到 Microsoft Access 数据库中。

当我单击按钮 btnOrderProducts 时,我收到一条错误消息:

错误:从字符串“x”到整数类型的转换无效

我不确定此错误的含义以及如何找出解决方案。将我的 access 数据库数据类型从整数更改为短文本并没有解决它,所以它与代码有关。

Here is an image of the form for reference

代码如下:

Private Sub btnOrderProducts_Click(sender As Object, e As EventArgs) Handles btnOrderProducts.Click

        Dim numberOfItems = ListBoxCart.Items.Count
        For Each item As String In ListBoxCart.Items

            Try

                cm = New OleDbCommand
                With cm
                    .Connection = cn
                    .CommandType = CommandType.Text
                    .CommandText = "INSERT INTO tblOrders ([ProductName],[Quantity],[PriceEach],[TotalPrice],[Username]) VALUES (@ProductName,@Quantity,@PriceEach,@TotalPrice,@Username)"

                    prod_list = item.Split(" ").ToList
                    Dim prod_name = prod_list.ElementAt(1)
                    Dim prod_quantity = Integer.Parse(prod_list.ElementAt(0).Remove("x"))
                    Dim price_each = prod_list.ElementAt(2)

                    .Parameters.Add(New OleDbParameter("@ProductName", OleDbType.VarChar, 255, prod_name))
                    .Parameters.Add(New OleDbParameter("@Quantity", OleDbType.VarChar, 255, prod_quantity))
                    .Parameters.Add(New OleDbParameter("@PriceEach", OleDbType.VarChar, 255, price_each))
                    .Parameters.Add(New OleDbParameter("@TotalPrice", OleDbType.VarChar, 255, Total))
                    .Parameters.Add(New OleDbParameter("@Username", OleDbType.VarChar, 255, txtUsername.Text))
                    prod_list = New List(Of String)

                    cm.Parameters("@ProductName").Value = prod_name
                    cm.Parameters("@Quanity").Value = prod_quantity
                    cm.Parameters("@PriceEach").Value = price_each
                    cm.Parameters("@TotalPrice").Value = Total
                    cm.Parameters("@Username").Value = txtUsername.Text

                    cm.ExecuteNonQuery()
                    MsgBox("Record saved.", MsgBoxStyle.Information)
                    cmboxProduct.SelectedItem = ""
                    txtQuantity.Text = ListBoxCart.Text = ""
                    txtTotalPrice.Text = ""

                    Exit Sub
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try

        Next

    End Sub

【问题讨论】:

  • 还要考虑在空间拆分将从“x1 iPad Case $20”创建一个由四个字符串组成的数组,其中价格位于索引 3 而不是 2

标签: vb.net


【解决方案1】:

此行不会从字符串中删除“x”

Dim prod_quantity = Integer.Parse(prod_list.ElementAt(0).Remove("x"))

应该是:

Dim prod_quantity = Integer.Parse(prod_list.ElementAt(0).Remove(0,1))

但我不建议将 items-properties 连接到一个字符串,然后拆分字符串以获取 items-properties。 给我一个机会回答你之前的问题。所以你不会有这些问题。

此外,我建议在项目属性中将Option Strict 设置为On

【讨论】:

  • 谢谢朋友!你为我修好了
【解决方案2】:

不幸的是,String.Remove 方法并没有像您认为的那样做。

来自链接:

返回一个新字符串,其中当前实例中的所有字符(从指定位置开始到最后一个位置)都已被删除

“x”不能转换成整数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多