title: Errors in inserting data to database
body: <p>I'm doing a project to develop a library management system to a library. In it, I'm registering students by saving their details to the database. When a record is inserted at first it is saved without the given membership number.</p>
  
  <p><a href="https://i.stack.imgur.com/A68mQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A68mQ.png" alt="enter image description here"></a></p>
  
  <p>And then, when I start to save the next student details, I get an error message saying "Violation of PRIMARY KEY constraint."</p>
  
  <p><a href="https://i.stack.imgur.com/tqoyk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tqoyk.png" alt="enter image description here"></a></p>
  
  <p>After I click the OK button in the message and try to save the data once more, I get a message saying, "Connection was not closed".</p>
  
  <p><a href="https://i.stack.imgur.com/wuJZj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wuJZj.png" alt="enter image description here"></a></p>
  
  <p>Although I try to find unclosed connection, I can't find one in my code.Here is the code.</p>
  
  <pre><code>        try
          {
              if (rbtnMale.Checked == true)
              {
                  Gender = "Male";
              }
              else if (rbtnFemale.Checked == true)
              {
                  Gender = "Female";
              }
  
              if (cmbMemNo.Visible == true)
              {
                  String insert_query = "INSERT INTO StReg VALUES('" + cmbMemNo.Text + "','" + txtFName.Text + "','" + txtName.Text + "','" + Gender + "','" + dtpDOB.Text + "','" + txtTelNo.Text + "','" + txtSchool.Text + "','" + txtAdNo.Text + "','" + txtMom.Text + "','" + txtMomOcc.Text + "','" + txtDad.Text + "','" + txtDadOcc.Text + "')";
                  Con.Open();
                  Cmd = new SqlCommand(insert_query, Con);
                  Cmd.ExecuteNonQuery();
                  Con.Close();
                  MessageBox.Show("The new Student " + txtName.Text + "( S-" + cmbMemNo.Text + " ) has successfully inserted into the system!!!", "INSERTED!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  Clear();
              }
              else if (txtMemNo.Visible == true)
              {
                  String insert_query = "INSERT INTO StReg VALUES('" + cmbMemNo.Text + "','" + txtFName.Text + "','" + txtName.Text + "','" + Gender + "','" + dtpDOB.Text + "','" + txtTelNo.Text + "','" + txtSchool.Text + "','" + txtAdNo.Text + "','" + txtMom.Text + "','" + txtMomOcc.Text + "','" + txtDad.Text + "','" + txtDadOcc.Text + "')";
                  Con.Open();
                  Cmd = new SqlCommand(insert_query, Con);
                  Cmd.ExecuteNonQuery();
                  Con.Close();
                  MessageBox.Show("The new Student " + txtName.Text + "( S-" + cmbMemNo.Text + " ) has successfully inserted into the system!!!", "INSERTED!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  Clear();
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error while Inserting details to the Database!!!" + Environment.NewLine + ex);
          }
  </code></pre>
  
  <p>This is my table design.</p>
  
  <p><a href="https://i.stack.imgur.com/5O5sa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5O5sa.png" alt="enter image description here"></a></p>
  
  <p>And here is my table definition code.</p>
  
  <pre><code>CREATE TABLE [dbo].[StReg]
  (
      [MemNo]   VARCHAR(12)  NOT NULL,
      [FName]   VARCHAR(MAX) NOT NULL,
      [Name]    VARCHAR(50)  NOT NULL,
      [Gender]  VARCHAR(6)   NOT NULL,
      [DOB]     DATE         NOT NULL,
      [TelNo]   VARCHAR(10)  NULL,
      [School]  VARCHAR(50)  NOT NULL,
      [AdNo]    VARCHAR(10)  NOT NULL,
      [MomName] VARCHAR(50)  NOT NULL,
      [MomOcc]  VARCHAR(50)  NOT NULL,
      [DadName] VARCHAR(50)  NOT NULL,
      [DadOcc]  VARCHAR(50)  NOT NULL,
  
      PRIMARY KEY CLUSTERED ([MemNo] ASC)
  );
  </code></pre>
  
  <p>No matter how hard I try to find any error, I still cannot figure it out. Please help!</p>