title: How do I convert the result of ExecuteScalar() to int?
body: <p>I am actually trying to get the primary key after the insertion by using <code>ExecuteScalar()</code>. Since it returns the first column of the first row after the insertion. But I am getting 0. I do not know why it is happening. Please help me out.</p>
  
  <pre><code>query = "Insert into Admissions(Admission_date, Student_name, Father_name, Mother_name, DOB, Gender, Address, State, City, Pincode, Admission_for, Previous_school, Fees) values ('" + txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" + txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" + txtState.Text + "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" + txtPreviousSchool.Text + "','" + txtFees.Text + "')";
  
  cmd = new SqlCommand(query, con);
  con.Open();
  
  int admid = Convert.ToInt32(cmd.ExecuteScalar());
  </code></pre>