title: Issues with Increment MS-SQL, C#
body: <p>I am having an issue with the increment for the ID. The ID would increase by one every time I click insert, but the problem occurs when the ID 2, it would insert the values twice, if ID 3, it would insert the values three times, and so on.</p>
  
  <p>There are couple of options that I have been trying. One is Max and the other one is finding the last inserted value and add one to the ID just.</p>
  
  <p>I would appreciate if anyone can help me out with this. Thanks</p>
  
  <pre><code>public partial class LoginInfo : System.Web.UI.Page
  {
      static string myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  
      private void GenerateID()
      {
          SqlConnection myConnection = new SqlConnection(myConnectionString);
          string myQuery1 = "Select Count(S_ID) from Student_Name";
          SqlCommand cmd = new SqlCommand(myQuery1, myConnection);
          myConnection.Open();
          int addOneS_ID_Table1 = Convert.ToInt32(cmd.ExecuteScalar());
          myConnection.Close();
          addOneS_ID_Table1++;
          lblstdID.Text = addOneS_ID_Table1.ToString();
  
          myConnection.Open();
          cmd.CommandText = "Select Count(P_ID) from Student_Pass";
          int addOneP_ID_Table2 = Convert.ToInt32(cmd.ExecuteScalar());
          myConnection.Close();
          addOneP_ID_Table2++;
          lblstdPass.Text = addOneP_ID_Table2.ToString();
          /*-----------------------------------------------------------------*/
          //SqlConnection myConnection = new SqlConnection(myConnectionString);
  
          //SqlCommand cmd = new SqlCommand("SELECT MAX(S_ID) as max_S_ID from Student_Name",myConnection);
          //cmd.CommandType = CommandType.Text;
          //myConnection.Open();
          //lblstdID.Text = Convert.ToString(cmd.ExecuteScalar());
          //cmd.CommandText = "SELECT MAX(P_ID) as max_P_ID FROM Student_Pass";
          //lblstdPass.Text = Convert.ToString(cmd.ExecuteScalar());
          //myConnection.Close();
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if(!IsPostBack)
          {
              GenerateID();
          }
      }
  
      protected void btnInsert_Click(object sender, EventArgs e)
      {
          SqlConnection myConnection = new SqlConnection(myConnectionString);
  
          string myQuery = "Insert into Student_Name(S_ID,STUDENT_NAME) VALUES" + "(@S_ID,@STUDENT_NAME)";
  
          SqlCommand cmd = new SqlCommand(myQuery,myConnection);
  
          cmd.Parameters.Add("@S_ID", SqlDbType.Int).Value = lblstdID.Text;
          cmd.Parameters.Add("@STUDENT_NAME", SqlDbType.VarChar).Value = txtstdName.Text;
  
          if(myConnection.State == ConnectionState.Closed)
          {
              myConnection.Open();
          }
  
          cmd.ExecuteNonQuery();
          cmd.Parameters.Clear();
  
          //Second Table
  
          cmd.CommandText = "Insert into Student_Pass(P_ID,PASSWORD) VALUES" + "(@P_ID,@PASSWORD)";
          cmd.CommandType = CommandType.Text;
  
          cmd.Parameters.Add("@P_ID", SqlDbType.Int).Value = lblstdPass.Text;
          cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = txtStdPass.Text;
  
          cmd.ExecuteNonQuery();
          cmd.Parameters.Clear();
          myConnection.Close();
          GenerateID();
          lblResult.Text = "Successfully Saved";
          GridView1.DataBind();
      }
  }
  </code></pre>