 |
|
ปัญหาคำสั่งUpdate Object reference not set to an instance of an object. |
|
 |
|
|
 |
 |
|
ผู้ร้ายตัวจริงอาจไม่ใช่ code ที่ให้มานี้ครับ ลองเอาโค้ดส่วนอื่นมาดู
แต่ถ้าจะ error ก็จะมาจาก ค่าจาก textbox ที่ป้อนเข้ามา
|
 |
 |
 |
 |
Date :
2010-01-07 14:19:14 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Partial Class Purchase_Default
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Dim CusID As String = ""
End Sub
Protected Sub gvManageBindData()
strSQL = "SELECT * FROM CUS"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
gvManage.DataSource = dtReader
gvManage.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub dvEditBindData()
strSQL = "SELECT * FROM Cus where CUSID = '" & CUSID & "' "
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
dvEdit.DataSource = dtReader
dvEdit.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
End Class
อันนี้codeทั้งหมด กดปุ่มupdateมันบอกว่ามีปัญหาตรงcode update error คือ ที่บอกไปตอนแรก
|
 |
 |
 |
 |
Date :
2010-01-07 14:35:21 |
By :
topflight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดู
Code (VB.NET)
strSQL = "Update [Book] SET [BookISBN] = @txtISBN, "
strSQL &= "[BookName] = @txtName, "
strSQL &= "[BookDesc] = @txtDesc, "
strSQL &= "[BookPrice] = @txtPrice, "
strSQL &= "[BookQuantity] = @txtQuantity "
strSQL &= "WHERE [BookID] = @BookID"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@txtISBN", SqlDbType.NVarChar)
objCmd.Parameters.Add("@txtName", SqlDbType.NVarChar)
objCmd.Parameters.Add("@txtDesc", SqlDbType.NVarChar)
objCmd.Parameters.Add("@txtPrice", SqlDbType.NVarChar)
objCmd.Parameters.Add("@txtQuantity", SqlDbType.NVarChar)
objCmd.Parameters.Add("@BookID", SqlDbType.NVarChar)
objCmd.Parameters("@txtISBN").Value = txtISBN.Text
objCmd.Parameters("@txtName").Value = txtName.Text
objCmd.Parameters("@txtDesc").Value = txtDesc.Text
objCmd.Parameters("@txtPrice").Value = txtPrice.Text
objCmd.Parameters("@txtQuantity").Value = txtQuantity.Text
objCmd.Parameters("@BookID").Value = BookID
objCmd.ExecuteNonQuery()
|
 |
 |
 |
 |
Date :
2010-01-07 15:04:45 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
objCmd.Parameters("@txtQuantity").Value = txtQuantity.Text
error
Object reference not set to an instance of an object.
|
 |
 |
 |
 |
Date :
2010-01-07 15:54:08 |
By :
topflight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไข โค้ด ตรงส่วน Update นะครับ
ให้ประ การตัวแปร private sb as new stringbulder เป็น gobal นะครับ
sb.remove(0,sb.leght)
sb.appand("update Book ")
sb.appand(" set BookISBN = '" & txtISBN.text.replace("'","''") & "',")
sb.appand(" BookName = '" & txtBookName.text.replace("'","''") & "',")
sb.appand(" BookDesc = '" & txtBookDesc .text.replace("'","''") & "',")
sb.appand(" BookPrice = '" & txtBookPrice.text.replace("'","''") & "',")
sb.appand(" BookQuantity = '" & BookQuantity.text.replace("'","''") & "',")
sb.appand(" where (BookID = '" & BookID & "')")
strSQL = sb.tostring
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
dvEdit.ChangeMode(DetailsViewMode.ReadOnly)
dvEdit.Visible = False
gvManage.Visible = True
gvManageBindData()
'** ระวัง type ในดาต้าเบสด้วนนะครับว่าเก็บเป็นอะไร ถ้าเป็น int
ก็ convert ให้มันเป็น int ก่อน เช่น cint(BookQuantity.text.replace("'","''") )
หรือ เป็น float ก็เป็น cdbl(txtBookPrice.text.replace("'","''") )
|
 |
 |
 |
 |
Date :
2010-01-07 16:29:49 |
By :
lee_latee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่tungman ได้แล้ว นิดเดียวจริงๆ ไปcheck ที่ find controlsใหม่ มันหา txtBookQuantity ไม่เจอไหมเพราะชื่อไม่ตรงกัน code ที่ทำตอนเเรกก็ใช้ได้แล้ว
แล้วถ้าจะให้detailviewโชว์ ปุ่มupdateและcancelไม่ต้องโชว์Edit ในcommandfield ได้ไม
ขอบคุณพี่tungmanและlee_lateeมากๆ ที่ช่วยตอบ
|
 |
 |
 |
 |
Date :
2010-01-07 18:06:53 |
By :
topflight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีปัญหาตรงคำสั่งdelete
The DELETE statement conflicted with the REFERENCE constraint "FK_PurchaseDetail_Book". The conflict occurred in database "Bookshop", table "dbo.PurchaseDetail", column 'BookID'.
เหรอต้องเขียนโค้ดให้ลบในตารางอื่นด้วยเพราะมันอ้างกันอยู่เป็นฟอเรนคีย์
|
 |
 |
 |
 |
Date :
2010-01-07 18:23:30 |
By :
topflight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็นผมจะสั่งลบทีละตาราง
|
 |
 |
 |
 |
Date :
2010-01-07 18:40:54 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็ลบตารางทีละตาราง แต่มันอ้างกันอยู่เลยลบไม่ได้
หรือโค้ดผิดอยู่
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
|
 |
 |
 |
 |
Date :
2010-01-07 19:09:17 |
By :
topflight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|