You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新数据表时提示字符串转Double类型无效 输入字符串格式不正确求解决

错误根因定位

  • 核心触发点是VB.NET中字符串拼接符号混用:代码中拼接大部分字段用的是&(VB专用字符串拼接符,会自动把非字符串类型转成字符串再拼接),但到Qty和Rate两个字段时改用了+号。+号在VB中同时支持数值加法和字符串拼接,当两侧类型不一致时,会默认尝试把字符串转成Double做数值运算,就会触发你遇到的「字符串转Double类型无效」报错。
  • 次要风险点:如果DataGrid对应单元格为空、或者值不是合法数字,直接取Value或者调用Int()转换时也会触发类型错误。
  • 额外逻辑问题:执行UPDATE语句误用了ExecuteReader方法,该方法是用于查询返回结果集的,更新操作应该用ExecuteNonQuery。

快速修复方案

把Query1拼接语句中Qty和Rate行的+全部替换为&即可临时解决报错:

' 替换前的错误写法
Qty='" + (DataGridStock.Rows(J).Cells(6).Value) + "',Rate='" + Int(DataGridStock.Rows(J).Cells(8).Value) + "'
' 替换后的正确写法
Qty='" & (DataGridStock.Rows(J).Cells(6).Value) & "',Rate='" & Int(DataGridStock.Rows(J).Cells(8).Value) & "'

推荐优化方案(解决类型问题+防SQL注入)

直接拼接SQL字符串存在SQL注入风险,更稳妥的方式是使用参数化查询,同时也能从根本上避免类型转换问题,示例写法如下:

Private Sub UpdateInventory()
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("MainConString").ConnectionString
    Dim Result As DialogResult = MessageBox.Show(" Update this Record ?", "Save ! ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If Result <> DialogResult.Yes Then Exit Sub

    Using con As New SqlConnection(connectionString)
        con.Open()
        For J As Integer = 0 To DataGridStock.Rows.Count - 1
            ' 提前校验数值类型字段合法性
            Dim qty As Double = 0
            Double.TryParse(DataGridStock.Rows(J).Cells(6).Value?.ToString(), qty)
            Dim rate As Integer = 0
            Integer.TryParse(DataGridStock.Rows(J).Cells(8).Value?.ToString(), rate)

            Dim query As String = "Update Tempstock Set Type=@Type, Prefix=@Prefix, Srl=@Srl, DOcDate=@DOcDate, Sno=@Sno, Hsncode=@Hsncode, Branch=@Branch, Taxcode=@Taxcode, Code=@Code, Qty=@Qty, Rate=@Rate, Unit=@Unit"
            Using cmd As New SqlCommand(query, con)
                ' 逐个添加参数,自动适配类型
                cmd.Parameters.AddWithValue("@Type", LabelTrancode.Text)
                cmd.Parameters.AddWithValue("@Prefix", Lblprefix.Text)
                cmd.Parameters.AddWithValue("@Srl", TextINVNo.Text)
                cmd.Parameters.AddWithValue("@DOcDate", DateTime.Parse(TextBoxdocdate.Text)) ' 日期类型也可以提前校验合法性
                cmd.Parameters.AddWithValue("@Sno", DataGridStock.Rows(J).Cells(0).Value)
                cmd.Parameters.AddWithValue("@Hsncode", DataGridStock.Rows(J).Cells(4).Value)
                cmd.Parameters.AddWithValue("@Branch", LBLBranchcode.Text)
                cmd.Parameters.AddWithValue("@Taxcode", DataGridStock.Rows(J).Cells(5).Value)
                cmd.Parameters.AddWithValue("@Code", DataGridStock.Rows(J).Cells(2).Value)
                cmd.Parameters.AddWithValue("@Qty", qty)
                cmd.Parameters.AddWithValue("@Rate", rate)
                cmd.Parameters.AddWithValue("@Unit", DataGridStock.Rows(J).Cells(7).Value)
                ' 执行更新语句用ExecuteNonQuery
                cmd.ExecuteNonQuery()
            End Using
        Next
        MessageBox.Show(" Stock Updated")
    End Using
End Sub

内容的提问来源于stack exchange,提问作者Sanjay Jarad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 05:48:03