0

So I'm just practicing my c# skills here, is there a way where we could somewhat "bypass" the primary key not null requirement, yet the the primary Key(StudentID) is supposed to automatically enter it's own value and auto increment. I've already enabled the auto increment.

Here's what I've done to my code.

con.Open();
        string query_insert = "INSERT INTO StudentInfo_Tab (StudentName, Address, Age, Contact) VALUES('" + TextBox2.Text + "','" + DropDownList1.SelectedValue + "','" + double.Parse(TextBox4.Text) + "','" + TextBox5.Text + "')";
        SqlCommand comm = new SqlCommand(query_insert, con);
        SqlCommand command_Select = new SqlCommand(sql_SelectStudentID, con);
        SqlDataReader reader = command_Select.ExecuteReader();

        reader.Read();
        string a = reader["StudentID"].ToString();
        TextBox1.Text = null;
        for (int i = 0; i < 1; i++)
        {
            string val = a.Substring(1, a.Length - 1);
            int newnumber = Convert.ToInt32(val) + 1;
            a = "B" + newnumber.ToString("000");
        }
        TextBox1.Text = a;

        comm.ExecuteNonQuery();
        con.Close();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully added!');", true);

But this still requires me to enter a value into the StudentID textbox. I was hoping of just leaving the studentID Textbox, and have the database itself enter it's own value by using the auto increment of the database.

Jimcl
  • 1

0 Answers0