I am trying to save data from a form into a SQL database and in this case I can't use a stored procedure, which I think would take care of the problem.
Anyway I am trying to save a new record back to the database and I having a problem because the first field is an identity field set to auto increment. I found a sample online, which using a SET IDENTITY_INSERT tblName ON (it was a very clear sample) but I haven't been able to get it to work with my code. This is what I have so far:
By the way the identity field is called uID
VB Code:
Try Con.Open() trn = Con.BeginTransaction cmd.Connection = Con cmd.Transaction = trn With cmd .CommandText = _ "SET IDENTITY_INSERT tblRewards ON; " & _ "Insert Into tblRewards(" & _ "LastName, " & _ "FirstName, " & _ "Address1, " & _ "Address2, " & _ "City, " & _ "State, " & _ "ZIP, " & _ "Phone, " & _ "Email" & _ ") Values(" & _ "'" & strLastName & "', " & _ "'" & strLastName & "', " & _ "'" & strAddress1 & "', " & _ "'" & strAddress2 & "', " & _ "'" & strCity & "', " & _ "'" & strState & "', " & _ "'" & strZip & "', " & _ "'" & strPhone & "', " & _ "'" & strEmail & "')" .ExecuteNonQuery() trn.Commit() End With Catch ex As Exception MessageBox.Show(ex.Message & vbNewLine & ex.Source) Finally Con.Close() Con.Dispose() trn.Dispose() End Try




Reply With Quote