Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL and VB.NET identity insert problem.

SQL and VB.NET identity insert problem.

Scheduled Pinned Locked Moved Database
helpcsharpdatabase
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aqzman_
    wrote on last edited by
    #1

    Hi guys, I hope this is the right section, but as it's more of a SQL problem, it most likly is. Anyway, my problem is that I get this error: "DEFAULT or NULL are not allowed as explicit identity values." Everytime I try to run my code, but of all the searhing I've done on Google and Google code search I really can't find anything amiss. I'll post my source (What I think is needed) at the bottem. If there are any important lines of code that I've left out ask and I'll promptly post them.

        Dim mySelectQuery As String = "SELECT \* FROM tblCallers SET IDENTITY\_INSERT tblCallers ON"
        Dim myInsertQuery As String = "INSERT INTO tblCallers (CallerID, Name, Address, PostalCode, City, PhoneNumber, EmailAddress) VALUES(DEFAULT, '" & Name & "', '" & Address & "', '" & PostalCode & "', '" & City & "', '" & PhoneNum & "', '" & EmailAddress & "')"
        Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
        Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
        Dim myCommand2 As New Data.SqlClient.SqlCommand(myInsertQuery, myConnection)
        Dim retvalue As Integer
        myConnection.Open()
        retvalue = myCommand2.ExecuteNonQuery()
        Console.WriteLine(retvalue)
        Dim myReader As Data.SqlClient.SqlDataReader
        myReader = myCommand.ExecuteReader()
        myReader.Close()
        myConnection.Close()
    

    I know that the code works because I use took it from another program I wrote and added the "SET IDENTITY_INSERT tblCallers ON" line. Also, the CallerID field is an Identity field. Thanks, aqzman

    C G 2 Replies Last reply
    0
    • A aqzman_

      Hi guys, I hope this is the right section, but as it's more of a SQL problem, it most likly is. Anyway, my problem is that I get this error: "DEFAULT or NULL are not allowed as explicit identity values." Everytime I try to run my code, but of all the searhing I've done on Google and Google code search I really can't find anything amiss. I'll post my source (What I think is needed) at the bottem. If there are any important lines of code that I've left out ask and I'll promptly post them.

          Dim mySelectQuery As String = "SELECT \* FROM tblCallers SET IDENTITY\_INSERT tblCallers ON"
          Dim myInsertQuery As String = "INSERT INTO tblCallers (CallerID, Name, Address, PostalCode, City, PhoneNumber, EmailAddress) VALUES(DEFAULT, '" & Name & "', '" & Address & "', '" & PostalCode & "', '" & City & "', '" & PhoneNum & "', '" & EmailAddress & "')"
          Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
          Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
          Dim myCommand2 As New Data.SqlClient.SqlCommand(myInsertQuery, myConnection)
          Dim retvalue As Integer
          myConnection.Open()
          retvalue = myCommand2.ExecuteNonQuery()
          Console.WriteLine(retvalue)
          Dim myReader As Data.SqlClient.SqlDataReader
          myReader = myCommand.ExecuteReader()
          myReader.Close()
          myConnection.Close()
      

      I know that the code works because I use took it from another program I wrote and added the "SET IDENTITY_INSERT tblCallers ON" line. Also, the CallerID field is an Identity field. Thanks, aqzman

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      aqzman_ wrote:

      I know that the code works because I use took it from another program

      Uh-huh...

      aqzman_ wrote:

      Dim myInsertQuery As String = "INSERT INTO tblCallers (CallerID, Name, Address, PostalCode, City, PhoneNumber, EmailAddress) VALUES(DEFAULT, '" & Name & "', '" & Address & "', '" & PostalCode & "', '" & City & "', '" & PhoneNum & "', '" & EmailAddress & "')"

      You may wish to read about SQL Injection Attacks and how to prevent them[^] becuase your code is vulnerable right now. My suggestion, don't include the column that will automatically receive the identity in the list of columns you are inserting.


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

      A 1 Reply Last reply
      0
      • A aqzman_

        Hi guys, I hope this is the right section, but as it's more of a SQL problem, it most likly is. Anyway, my problem is that I get this error: "DEFAULT or NULL are not allowed as explicit identity values." Everytime I try to run my code, but of all the searhing I've done on Google and Google code search I really can't find anything amiss. I'll post my source (What I think is needed) at the bottem. If there are any important lines of code that I've left out ask and I'll promptly post them.

            Dim mySelectQuery As String = "SELECT \* FROM tblCallers SET IDENTITY\_INSERT tblCallers ON"
            Dim myInsertQuery As String = "INSERT INTO tblCallers (CallerID, Name, Address, PostalCode, City, PhoneNumber, EmailAddress) VALUES(DEFAULT, '" & Name & "', '" & Address & "', '" & PostalCode & "', '" & City & "', '" & PhoneNum & "', '" & EmailAddress & "')"
            Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
            Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
            Dim myCommand2 As New Data.SqlClient.SqlCommand(myInsertQuery, myConnection)
            Dim retvalue As Integer
            myConnection.Open()
            retvalue = myCommand2.ExecuteNonQuery()
            Console.WriteLine(retvalue)
            Dim myReader As Data.SqlClient.SqlDataReader
            myReader = myCommand.ExecuteReader()
            myReader.Close()
            myConnection.Close()
        

        I know that the code works because I use took it from another program I wrote and added the "SET IDENTITY_INSERT tblCallers ON" line. Also, the CallerID field is an Identity field. Thanks, aqzman

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        If CallerID is auto-increment column then you don't need to specify it's value

        #region signature my articles #endregion

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          aqzman_ wrote:

          I know that the code works because I use took it from another program

          Uh-huh...

          aqzman_ wrote:

          Dim myInsertQuery As String = "INSERT INTO tblCallers (CallerID, Name, Address, PostalCode, City, PhoneNumber, EmailAddress) VALUES(DEFAULT, '" & Name & "', '" & Address & "', '" & PostalCode & "', '" & City & "', '" & PhoneNum & "', '" & EmailAddress & "')"

          You may wish to read about SQL Injection Attacks and how to prevent them[^] becuase your code is vulnerable right now. My suggestion, don't include the column that will automatically receive the identity in the list of columns you are inserting.


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

          A Offline
          A Offline
          aqzman_
          wrote on last edited by
          #4

          Thanks a lot for the reply! I know my code might be a bit shakey, but I only started learning SQL, ASP.NET 2.0, and Visual Basic .NET last week. So sorry for any newbie mistakes, heh. Thanks for the advice about SQL Injection, I've only ever heard of it. Never read about it myself. This program will only be going up on a in-office intranet though, so I'm not too worried about attackers. But I'll probably fix it up once I read the links you posted. Never too early to start learning good programming habits. As for not including the column which will recieve the identity, that did the trick! Thanks a lot for your help! aqzman

          C 1 Reply Last reply
          0
          • A aqzman_

            Thanks a lot for the reply! I know my code might be a bit shakey, but I only started learning SQL, ASP.NET 2.0, and Visual Basic .NET last week. So sorry for any newbie mistakes, heh. Thanks for the advice about SQL Injection, I've only ever heard of it. Never read about it myself. This program will only be going up on a in-office intranet though, so I'm not too worried about attackers. But I'll probably fix it up once I read the links you posted. Never too early to start learning good programming habits. As for not including the column which will recieve the identity, that did the trick! Thanks a lot for your help! aqzman

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            aqzman_ wrote:

            This program will only be going up on a in-office intranet though, so I'm not too worried about attackers.

            Statistically, most attacks are from people within the organisation.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

            S 1 Reply Last reply
            0
            • C Colin Angus Mackay

              aqzman_ wrote:

              This program will only be going up on a in-office intranet though, so I'm not too worried about attackers.

              Statistically, most attacks are from people within the organisation.


              Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

              S Offline
              S Offline
              sidbaruah
              wrote on last edited by
              #6

              true very true!!! :)

              I was born dumb!! :laugh:Programming made me laugh:laugh:!!! --sid--

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups