 |
|
Add ข้อมูลเพิ่มแล้วมันขึ้นว่า IDENTITY_INSERT is set to OFF. |
|
 |
|
|
 |
 |
|
สร้างเรคคอร์ดสำหรับ add ข้อมูลเพิ่มไว้อ่าค่ะ
แต่พอลองเทสท์เพิ่มข้อมูลลงไป
พอกดปุ่ม add ปุ๊ป ก้อขึ้นว่า
Cannot insert explicit value for identity column in table 'PLASMA' when IDENTITY_INSERT is set to OFF.
แล้วก้อจะมีกรอบเน้นขึ้นตรงโค้ดนี้
objCmd.ExecuteNonQuery()
ต้องแก้ตรงไหนเหรอคะ??
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
Dim strConnString As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
objConn = New SqlConnection(strConnString)
objConn.Open()
IF Not Page.IsPostBack() Then
BindData()
End IF
End Sub
Sub BindData()
strSQL = "SELECT * FROM PLASMA"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to GridView ***'
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
Sub modEditCommand(sender As Object, e As GridViewEditEventArgs)
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
BindData()
End Sub
Sub modCancelCommand(sender As Object, e As GridViewCancelEditEventArgs)
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
Sub modDeleteCommand(sender As Object, e As GridViewDeleteEventArgs)
strSQL = "DELETE FROM PLASMA WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Sub myGridView_RowCommand(source As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** CustomerID ***'
Dim txtPartID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartID"), TextBox)
'*** Email ***'
Dim txtPartNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartNo"), TextBox)
'*** Name ***'
Dim txtPartName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartName"), TextBox)
'*** CountryCode ***'
Dim txtPartModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartModel"), TextBox)
'*** Budget ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCustomerPrice"), TextBox)
'*** Used ***'
Dim txtPartSub As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartSub"), TextBox)
strSQL = "INSERT INTO PLASMA (PartID,PartNo,PartName,PartModel,CustomerPrice,PartSub) " & _
" VALUES ('" & txtPartID.Text & "','" & txtPartNo.Text & "','" & txtPartName.Text & "' " & _
" ,'" & txtPartModel.Text & "','" & txtCustomerPrice.Text & "','" & txtPartSub.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Sub modUpdateCommand(s As Object, e As GridViewUpdateEventArgs)
'*** CustomerID ***'
Dim txtPartID As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartID"), TextBox)
'*** Email ***'
Dim txtPartNo As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartNo"), TextBox)
'*** Name ***'
Dim txtPartName As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartName"), TextBox)
'*** CountryCode ***'
Dim txtPartModel As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartModel"), TextBox)
'*** Budget ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCustomerPrice"), TextBox)
'*** Used ***'
Dim txtPartSub As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPartSub"), TextBox)
strSQL = "UPDATE PLASMA SET PartID = '" & txtPartID.Text & "' " & _
" ,PartNo = '" & txtPartNo.Text & "' " & _
" ,PartName = '" & txtPartName.Text & "' " & _
" ,PartModel = '" & txtPartModel.Text & "' " & _
" ,CustomerPrice = '" & txtCustomerPrice.Text & "' " & _
" ,PartSub = '" & txtPartSub.Text & "' " & _
" WHERE PartID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
</script>
Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), VB.NET
|
|
 |
 |
 |
 |
Date :
2010-08-29 17:42:18 |
By :
arpraew |
View :
1632 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทีหลังใส่ tag bb ให้หน่อยนะครับ
Code (VB.NET)
Sub myGridView_RowCommand(source As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Add" Then
'*** CustomerID ***'
Dim txtPartID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartID"), TextBox)
'*** Email ***'
Dim txtPartNo As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartNo"), TextBox)
'*** Name ***'
Dim txtPartName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartName"), TextBox)
'*** CountryCode ***'
Dim txtPartModel As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartModel"), TextBox)
'*** Budget ***'
Dim txtCustomerPrice As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCustomerPrice"), TextBox)
'*** Used ***'
Dim txtPartSub As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPartSub"), TextBox)
strSQL = "INSERT INTO [PLASMA[ ([PartID], [PartNo], [PartName], [PartModel], [CustomerPrice], [PartSub]) VALUES (@PartID, @PartNo, @PartName, @PartModel, @CustomerPrice, @PartSub)"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.AddWithValue("@PartID", txtPartID.Text)
objCmd.Parameters.AddWithValue("@PartNo", txtPartNo.Text)
objCmd.Parameters.AddWithValue("@PartName", txtPartName.Text)
objCmd.Parameters.AddWithValue("@PartModel", txtPartModel.Text)
objCmd.Parameters.AddWithValue("@CustomerPrice", txtCustomerPrice.Text)
objCmd.Parameters.AddWithValue("@PartSub", txtPartSub.Text)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
|
 |
 |
 |
 |
Date :
2010-08-29 18:06:16 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อะ ขอโทดทีค่ะ ไม่เปนงานง่ะ แหะๆ
พอดีแก้แล้วมันเออเร่อต่ออ่ะคะ
ชี้ตรงนี้..
objCmd.ExecuteNonQuery()
แล้วเออเร่อว่า
Incorrect syntax near ','.
|
 |
 |
 |
 |
Date :
2010-08-29 21:05:42 |
By :
arpraew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|