Search code examples
c#sql-serverauto-increment

C# Auto increment primary key


I have already set the IDENTITY in the database to yes and whenever i run the application, the key doesnt auto increment giving an error about does not allow null values

private void button8_Click(object sender, EventArgs e)
        {

            string fname = textBox1.Text;
            string lname = textBox2.Text;
            //int idnum = Convert.To(textBox3.Text);
            int mobnum = Convert.ToInt32(maskedTextBox1.Text);
            string email = textBox4.Text;

            int EdInst = comboBox1.SelectedIndex+1;
            string EdLev = comboBox3.SelectedText;
            string EdName = comboBox2.SelectedText;
            Boolean Valid = true;
            Valid = Validation(Valid);

            if (Valid == true)
            {
                IS2Team1_TriplexDBDataSet.ApplicantRow NewApplicantRow = iS2Team1_TriplexDBDataSet1.Applicant.NewApplicantRow();

                IS2Team1_TriplexDBDataSet.ApplicationRow NewApplicationRow = iS2Team1_TriplexDBDataSet1.Application.NewApplicationRow();

                //NewApplicantRow.Applicant_ID = ??;  // What do i do here for the ID to auto increment?
                NewApplicantRow.First_Name = fname;
                NewApplicantRow.Last_Name = lname;
                //NewApplicantRow.ID_Number =Convert.ToInt32(textBox3.Text);
                NewApplicantRow.Contact_Number = mobnum;
                NewApplicantRow.Email_Address = email;
                NewApplicantRow.University_ID = EdInst;

              //NewApplicationRow.Application_ID = ??; // What do i do here for the ID to auto increment?
                NewApplicationRow.Application_Status = "Recieved";
                NewApplicationRow.Application_Date = DateTime.Today;



                iS2Team1_TriplexDBDataSet1.Applicant.Rows.Add(NewApplicantRow);
                iS2Team1_TriplexDBDataSet1.Application.Rows.Add(NewApplicationRow);

                MessageBox.Show("Application Submitted", "Application Submitted");

                this.applicantTableAdapter.Update(this.iS2Team1_TriplexDBDataSet1);


                //Hide();
                //Form4 frmUpdate = new Form4();
                //frmUpdate.Show();

            }
                else if (Valid == false)
            {
                    MessageBox.Show("Required Information Missing");
                    textBox1.Focus();
            }

Solution

  • You have to set the following Value in your Table-Definition:

    Image

    Sample: Not For Entity-Framework:

    If you want to insert a new Row, simply ignore your Identity-Row and get the auto-generated Identity with: @@Identity

    Hope this help!

    Here is an UNTESTED example:

    public static void Test()
        {
            int cIdentity = 0;
    
            SqlConnection cConnection = new SqlConnection("...");
            cConnection.Open();
    
            SqlCommand cCommand = new SqlCommand("Insert INTO TestTable (Name) VALUES ('Test'); "+
                "SELECT @@IDENTITY AS Ident", cConnection);
    
            IDataReader cReader = null;
    
            try
            {
                cReader = cCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (cReader.Read())
                {
                    cIdentity = cReader.GetInt32(0);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
                if (cReader != null)
                {
                    cReader.Close();
                }
            }
            finally
            {
                if (cConnection != null)
                {
                    cConnection.Close();
                }
            }
        }