0

我有一个删除按钮,我试图通过单击该按钮从 gridview 和数据库中删除选定的行。但是有以下代码我得到了参数超出范围之类的错误并转换为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.SelectedRows(0).Index
        DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
        con = New SqlConnection(constring)
        con.Open()
        Me.StudentID = Convert.ToInt32(DataGridView1.SelectedRows(0).Index)
        cmd = New SqlCommand("Delete from KaiyumVbStudent where StudentID = '@StudentID'", con)
        cmd.ExecuteNonQuery()
        Call databind()

Private Sub databind()
        con = New SqlConnection(constring)
        con.Open()
        cmd = New SqlCommand("Select *from KaiyumVbStudent", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dt = New DataTable()
        dt.Load(dr)
        Me.DataGridView1.DataSource = dt
    End Sub
4

1 回答 1

0

首先将我们的代码放入 try catch 语句并检查学生 ID 数据类型您缺少一个语句 cmd.Parameters.AddWithValue("@StudentID", StudentID); 我希望你这是工作..

于 2012-09-27T05:29:32.660 回答