DataGrid的删除问题,在线恭候各位。
各位,
我在DataGrid中加入编辑、修改、删除,删除的代码是照着修改写的,
附代码如下:
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
If cn.State = ConnectionState.Closed Then cn.Open()
Dim oldName As String = CType(e.Item.FindControl("hdnName"), TextBox).Text
Dim oldIDCard As String = CType(e.Item.FindControl("hdnIDCard"), TextBox).Text
Dim strName As String = CType(e.Item.FindControl("txtName"), TextBox).Text
Dim strIDCard As String = CType(e.Item.FindControl("txtIDCard"), TextBox).Text
Dim strUpdate As String = "update tbEmployee set 姓名='" & Trim(strName) & "',身份证号='" & Trim(strIDCard) & "'" & _
" where 姓名=@Name and 身份证号=@ID "
Dim SqlTmp As New SqlCommand(strUpdate, cn)
SqlTmp.Parameters.Add(New SqlParameter("@Name", SqlDbType.NVarChar, 30)).Value = Trim(oldName)
SqlTmp.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 18)).Value = Trim(oldIDCard)
Try
SqlTmp.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message & "<br>")
End Try
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
If cn.State = ConnectionState.Closed Then cn.Open()
Dim oldName As String = CType(e.Item.FindControl("hdnName"), TextBox).Text
Dim oldIDCard As String = CType(e.Item.FindControl("hdnIDCard"), TextBox).Text
Dim strName As String = CType(e.Item.FindControl("txtName"), TextBox).Text
Dim strIDCard As String = CType(e.Item.FindControl("txtIDCard"), TextBox).Text
Dim strUpdate As String = "delete from tbEmployee where 姓名=@Name and 身份证号=@ID "
Dim SqlTmp As New SqlCommand(strUpdate, cn)
SqlTmp.Parameters.Add(New SqlParameter("@Name", SqlDbType.NVarChar, 30)).Value = Trim(oldName)
SqlTmp.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 18)).Value = Trim(oldIDCard)
Try
SqlTmp.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message & "<br>")
End Try
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
执行“修改”正确,但是,当点击“删除”按钮时,系统报错如下:
“未将对象引用设置到对象的实例。 ”
请各位帮忙看看是什么问题。谢了!