【问题标题】:How to edit and delete data in DataGridView?如何在 DataGridView 中编辑和删除数据?
【发布时间】:2018-07-17 14:41:08
【问题描述】:

我正在 VB.NET 中开发一个桌面应用程序,我想在其中编辑、删除和添加 DataGridView 中的新行。我怎样才能做到这一点?

出现错误“从字符串“Update dbo.Trans set BCode='1'”转换为“Double”类型无效。”

我试过的代码:

con = New SqlConnection("Data Source=USER\SQLEXPRESS;Initial Catalog=Sap;Integrated Security=True")

    If (TbCode.Text = "" Or TbItem.Text = "" Or TbWgt.Text = "") Then
        MessageBox.Show("Please fill all the columns")
    Else
        con.Open()

        cmd1 = New SqlCommand("Update  dbo.Trans set BCode='" + TbCode.Text + "',BName='" + Label2.Text + "',ICode='" + TbItem.Text + "',IName='" + Label3.Text + "',Qty='" + TbQty.Text + "',Weight='" + TbWgt.Text + "',RWeight='" + Label4.Text + "',Rate='" + Rate + "'  where date=@date and depot=@depot and BCode=@bcode and Icode=@icode", con)
        cmd1.Parameters.AddWithValue("@depot", TbDepot.Text)
        cmd1.Parameters.AddWithValue("@icode", TbItem.Text)
        cmd1.Parameters.AddWithValue("@date", IDate.Text)
        cmd1.Parameters.AddWithValue("@bcode", TbCode.Text)
        cmd1.ExecuteNonQuery()


    End If


    con.Close()

    Call Fillgrid()
    Call emptyfun()

End Sub

【问题讨论】:

  • id1 应该是您在 SQL 语句中想要的吗?因为你有身份证。为什么不使用 Date 和 Depot 那样的参数?您是否收到任何错误消息?缺少很多代码来正确设置和执行该 SqlCommand。
  • 看看this post
  • 先生,我尝试使用此代码通过检索文本框中的值来更新网格值。
  • 你的 Trans 表有主键吗?

标签: .net vb.net


【解决方案1】:

这里的问题是您使用加号来连接字符串并且应该使用与号。

如果您打开 Strict Option On,这将由编译器引起。

这是您更新后的代码。

cmd1 = New SqlCommand("Update  dbo.Trans set BCode='" & TbCode.Text & "',BName='" & Label2.Text & "',ICode='" & TbItem.Text & "',IName='" & Label3.Text & "',Qty='" & TbQty.Text & "',Weight='" & TbWgt.Text & "',RWeight='" & Label4.Text & "',Rate='" & Rate & "'  where date=@date and depot=@depot and BCode=@bcode and Icode=@icode", con)
        cmd1.Parameters.AddWithValue("@depot", TbDepot.Text)
        cmd1.Parameters.AddWithValue("@icode", TbItem.Text)
        cmd1.Parameters.AddWithValue("@date", IDate.Text)
        cmd1.Parameters.AddWithValue("@bcode", TbCode.Text)
        cmd1.ExecuteNonQuery()

但是,从技术上讲,您应该在整个更新语句中使用参数,而不仅仅是 where 子句。另一个建议是显式定义参数的数据类型,而不是使用 AddWithValue。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多