Can't insert a row to a table with identity on

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jake77
    New Member
    • Apr 2010
    • 2

    Can't insert a row to a table with identity on

    Hey

    I'm trying to nsert a raw to a table that has one column marked with identity ON.

    The code is like this:

    string sqlString1 = "INSERT into T_Branches (BranchTypeID, BranchName) VALUES (" + _BranchType + ", '" + _BranchName + "')";


    SqlConnection objConnection = new SqlConnection() ;
    SqlDataAdapter objAdapater = new SqlDataAdapter( );
    SqlCommand objCommand = new SqlCommand(sqlS tring1);
    objConnection.C onnectionString = connectionStrin g
    objConnection.O pen();
    objCommand.Conn ection = objConnection;
    objAdapater.Ins ertCommand = objCommand;
    objAdapater.Ins ertCommand.Exec uteNonQuery();

    For some reason I get the following error message:
    ---------------------------------------------------------------------------------------------------------
    Cannot insert the value NULL into column 'BranchID', table 'CAR_RENTAL.MDF .dbo.T_Branches '; column does not allow nulls. INSERT fails.
    The statement has been terminated.
    --------------------------------------------------------------------------------------------------------
    BranchID is the primary key and an identify column

    When I try to insert values manually I succeed and the value in BranchID increments automatically

    Any idea what am I doing wrong?
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Here, read this...

    Happy Coding!!!

    ~~ CK

    Comment

    • Jake77
      New Member
      • Apr 2010
      • 2

      #3
      Nope - that didn't do the trick

      I actually turned ALL table's Identity attribute to OFF except for the one in question.
      Still I get the same error.

      The question is why do I get a "Cant Insert NULL" error message when the values in this column should be automatically inserted and increased by the DB?

      Tnx!

      Comment

      • shek124
        New Member
        • Oct 2007
        • 50

        #4
        Originally posted by Jake77
        I actually turned ALL table's Identity attribute to OFF except for the one in question.
        Still I get the same error.

        The question is why do I get a "Cant Insert NULL" error message when the values in this column should be automatically inserted and increased by the DB?

        Tnx!
        hi,

        the primary key column does n't allow nulls value. So you have to insert the values in the column using insert query

        Comment

        Working...