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)
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)
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