Results 1 to 6 of 6

Thread: (Resolved) SET IDENTITY_INSERT tblName ON

  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

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: SET IDENTITY_INSERT tblName ON

    If you have an identity field, do not even mention that column in your insert statement. The database will handle the creation of that field automatically. You don't have to do anything special.
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SET IDENTITY_INSERT tblName ON

    I don't even see an ID field in the query.... what WAS the error you were getting?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: SET IDENTITY_INSERT tblName ON

    Originally I left the uID field out because I expected SQL Server to just increment it automatically for me like Access does. However when I ran it this is what I get:

    Cannot insert the value NULL into column 'uID', table
    'Rewards.dbo.tblRewards'; column does not allow nulls. INSERT fails.

    The statement has been terminated.

    This is the exact same SQL above minus the line about Set Identity. The error seems to me to indicate that it needs something for the unique field, which is why I went to Google and came up with the Set Identity.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SET IDENTITY_INSERT tblName ON

    Are you sure that uID field is an identity field or is it just a primary field and gets mistaken for identity field?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: SET IDENTITY_INSERT tblName ON

    stanav I was absolutely 100% sure that it was set as an Identity Field. There wasn't any doubt in my mind but I checked anyways, because you brought it up, and you nailed it. It wasn't. It is now and the code works fine.

    Thanks for pointing out the (not so) obvious to me.

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