HOME > .NET Framework > Forum > Delete ไม่ได้ ติดError NullReferenceException was unhandled by user code Object reference not set to an instance of an object.
Delete ไม่ได้ ติดError NullReferenceException was unhandled by user code Object reference not set to an instance of an object.
Dim lblID As Label = CType(gvManage.FindControl("lblID"), Label)
BookID = lblID.Text
strSQL = " DELETE FROM Book Where BookID = '" & BookID & "' "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
gvManageBindData()
มันขึ้นError
NullReferenceException was unhandled by user code
Object reference not set to an instance of an object.
ตรงBookID = lblID.Text
และถ้าใช้อีกโค้ดจะติดError SqlException was unhandled by user code
The DELETE statement conflicted with the REFERENCE constraint "FK_PurchaseDetail_Book". The conflict occurred in database "Bookshop", table "dbo.PurchaseDetail", column 'BookID'.
Code (VB.NET)
Protected Sub gvManage_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvManage.RowDeleting
strSQL = "DELETE FROM Book WHERE BookID = '" & gvManage.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
gvManageBindData()
End Sub