0
I am deleting selected row from grid view.

使用以下代码行从 gridview 中删除,但未从数据库中删除。它显示了将 varchar 转换为 int 的错误

Private Sub dltButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dltButton.Click
            'Dim StudentId As String
            'StudentId =
            'con.Open()
            'cmd.CommandText = "delete from KaiyumVbStudent where StudentID = '"&StudentId&"'"
            'cmd.Connection = con
            Dim i As Integer = DataGridView1.CurrentRow.Index
            DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
            StudentID = CType(DataGridView1.SelectedCells(2), TextBox).Text
            con = New SqlConnection(constring)
            con.Open()
            cmd = New SqlCommand("Delete from KaiyumVbStudent where StudentID = '@StudentID'", con)
            cmd.ExecuteNonQuery()
            Call databind()
        End Sub
4

2 回答 2

1

我怀疑这是因为您的参数包含引号。

也就是说,这个:

Delete from KaiyumVbStudent where StudentID = '@StudentID'

可能应该这样读(注意@StudentID周围没有引号):

Delete from KaiyumVbStudent where StudentID = @StudentID

但是,我看不到您在代码中为@StudentID.

于 2012-09-27T06:47:07.190 回答
0

执行 where 时您可能不会收到数据类型转换错误,StudentID='1'但请同意上面的评论,因为您似乎没有设置参数值。如此有效地执行以下 SQL:

Delete from KaiyumVbStudent where StudentID = '@StudentID'

于 2012-09-27T11:35:54.683 回答