Results 1 to 6 of 6

Thread: (Resolved) SET IDENTITY_INSERT tblName ON

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Resolved (Resolved) SET IDENTITY_INSERT tblName ON

    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:
    1. Try
    2.             Con.Open()
    3.             trn = Con.BeginTransaction
    4.             cmd.Connection = Con
    5.             cmd.Transaction = trn
    6.             With cmd
    7.                 .CommandText = _
    8.                     "SET IDENTITY_INSERT tblRewards ON; " & _
    9.                     "Insert Into tblRewards(" & _
    10.                     "LastName, " & _
    11.                     "FirstName, " & _
    12.                     "Address1, " & _
    13.                     "Address2, " & _
    14.                     "City, " & _
    15.                     "State, " & _
    16.                     "ZIP, " & _
    17.                     "Phone, " & _
    18.                     "Email" & _
    19.                     ") Values(" & _
    20.                     "'" & strLastName & "', " & _
    21.                     "'" & strLastName & "', " & _
    22.                     "'" & strAddress1 & "', " & _
    23.                     "'" & strAddress2 & "', " & _
    24.                     "'" & strCity & "', " & _
    25.                     "'" & strState & "', " & _
    26.                     "'" & strZip & "', " & _
    27.                     "'" & strPhone & "', " & _
    28.                     "'" & strEmail & "')"
    29.                 .ExecuteNonQuery()
    30.                 trn.Commit()
    31.             End With
    32.         Catch ex As Exception
    33.             MessageBox.Show(ex.Message & vbNewLine & ex.Source)
    34.         Finally
    35.             Con.Close()
    36.             Con.Dispose()
    37.             trn.Dispose()
    38.         End Try
    Last edited by Hack; Oct 25th, 2007 at 07:34 AM. Reason: Added green resolved checkmark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width