CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2005
    Location
    Netherlands, Assendelft
    Posts
    19

    Question Adding a record in SQL with an autonumber

    Hi guys,

    I'm trying to add a record in an SQL statement through an SqlCommand into an Sql table. This table has an Autonumber (ID) and is returning an error as soon as I try to add the record through ASP.Net (C#)

    Code:
    "INSERT INTO indicators(client_id, language_id, type, " +
    "location, sub_type, sub_type_desc, sub_type_category, used) " +
    "VALUES ('" + txtCust.Text + "','" + txtLanguage.SelectedValue + 
    "','" + txtType.SelectedValue + "','" + txtLocation.SelectedValue + 
    "','" + txtSubType.SelectedValue + "','" + txtSubDesc.Text + 
    "','" + txtSubCat.Text + "','" + txtUsed.SelectedValue + "')" +
    " SELECT * FROM indicators"
    This is generating the following error:
    Violation of PRIMARY KEY constraint 'PK_indicators'. Cannot insert duplicate key in object 'indicators'. The statement has been terminated.
    My SQL knowledge is very basic so can anyone please help me on this one?

    Thanks in advance!

    Vincent

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Adding a record in SQL with an autonumber

    As the field is an AutoNumber field, you do not have to explicitly mention it in your SQL INSERT Statement.. The field will be incremented by the SQL Server automatically..

    You need to remove the field that is set to Autonumber from your Query..

  3. #3
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: Adding a record in SQL with an autonumber

    because the ID is automatically populated, you don't need to populate it yourself.
    so, your statement would be:

    Code:
    INSERT INTO indicators(language_id, type, location, sub_type, sub_type_desc, sub_type_category, used)
    VALUES ('" + txtLanguage.SelectedValue + "','" +
                 txtType.SelectedValue + "','" + txtLocation.SelectedValue + "','" +
                 txtSubType.SelectedValue + "','" + txtSubDesc.Text + "','" +
                 txtSubCat.Text + "','" + txtUsed.SelectedValue + "')
    
     SELECT * FROM indicators"
    Make sense? The autonumber field will be automatically populated


    BTW:
    This is not a good way to use SQL statements. Using this technique your database can be very easily compremised. It is called SQL Insertion. Consider when somebody enters the following text into one of your text boxes:

    blah '); DROP indicators // "

    this text will be put as is into your string. The statement will then be interpreted as follows:
    The single quote (after blah) will end the string, and the insert will throw an error... HOWEVER, more problematic is, the new statment (DROP indicators) will delete your table. and everything after this will be treated as a comment.

    You should rather use stored procedures.

    Just thought you might like to know... rather than find out the hard way.
    Mike

  4. #4
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: Adding a record in SQL with an autonumber

    Just double check to make sure you have specified the autonumber on the primary key column.

  5. #5
    Join Date
    Mar 2005
    Location
    Netherlands, Assendelft
    Posts
    19

    Re: Adding a record in SQL with an autonumber

    Thank you for your quick responds guys, but I don't have the ID set up in the query (it's simply called id)
    Any other thoughts?

    Thanks again for all the replies!

  6. #6
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: Adding a record in SQL with an autonumber

    what is the primary key of your table?is it the ID column or is it ID+some other column?

  7. #7
    Join Date
    Apr 2005
    Posts
    576

    Re: Adding a record in SQL with an autonumber

    Quote Originally Posted by CoffeeMug
    Any other thoughts?
    To get the inserted id, use SCOPE_IDENTITY() in SQL Server. (@@IDENTITY returns the latest inserted id on the current connection, it is usually the correct id but if your insert statement caused some trigger to insert some other row then this id will be returned by @@IDENTITY).

    To use insert with data adapters, see this thread: http://www.codeguru.com/forum/showthread.php?t=344085

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Adding a record in SQL with an autonumber

    in oracle we do:


    insert into table1
    select max(id_number)+1, 'literal value for column 1', 'value for column 2', 'col 3' ...
    from table1


    the select statement returns 1 row that has:

    the max id number +1, followed by all the literal text you put in, in columns

    this row is then inserted. clever eh?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Mar 2005
    Location
    Netherlands, Assendelft
    Posts
    19

    Re: Adding a record in SQL with an autonumber

    Hey people,

    I found out what the problem was, the record I was using to test this with already existed in the database (DUH) which returned this error.
    Thanks for all your help though really appreciate it!

  10. #10
    Join Date
    Apr 2005
    Posts
    576

    Re: Adding a record in SQL with an autonumber

    Quote Originally Posted by cjard
    in oracle we do:


    insert into table1
    select max(id_number)+1, 'literal value for column 1', 'value for column 2', 'col 3' ...
    from table1


    the select statement returns 1 row that has:

    the max id number +1, followed by all the literal text you put in, in columns

    this row is then inserted. clever eh?
    I thought sequences were used in Oracle.

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

Featured