Hi.
I have written an insert statement with the below code:
protected void SubmitButton(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlTransaction trans = null;
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBMarashi"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
trans = cmd.Connection.BeginTransaction();
try
{
cmd.CommandText = "INSERT INTO Persons (FirstName,LastName,FatherName,PersonelCode,SerialNumber,NationalNumber,BirthDate_S,PassportNumber,Nationality,Phone,Mobile,PostalCode,Email,HomeAddress) values(@FirstName,@LastName,@FatherName,@PersonelCode,@SerialNumber,@NationalNumber,@BirthDate_S,@PassportNumber,@Nationality,@Phone,@Mobile,@PostalCode,@Email,@HomeAddress)";
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = FirstNameTextBox.Text;
cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastNameTextBox.Text;
cmd.Parameters.Add("@FatherName", SqlDbType.NVarChar).Value = FatherNameTextBox.Text;
cmd.Parameters.Add("@PersonelCode", SqlDbType.Int).Value = Convert.ToInt32(personelCodeTextBox.Text);
cmd.Parameters.Add("@SerialNumber", SqlDbType.Int).Value = Convert.ToInt32(SerialNumTextBox.Text);
cmd.Parameters.Add("@NationalNumber", SqlDbType.Int).Value = Convert.ToInt32(nationalNumTextBox.Text);
cmd.Parameters.Add("@BirthDate_S", SqlDbType.Date).Value = Convert.ToDateTime(BirthDateTextBox.Text);
cmd.Parameters.Add("@PassportNumber", SqlDbType.Int).Value = Convert.ToInt32(PassNumTextBox0.Text);
cmd.Parameters.Add("@Nationality", SqlDbType.Int).Value = Convert.ToInt32(NationalityTextBox.Text);
cmd.Parameters.Add("@Phone", SqlDbType.Int).Value = Convert.ToInt32(PhoneTextBox0.Text);
cmd.Parameters.Add("@Mobile", SqlDbType.Int).Value = Convert.ToInt32(MobileTextBox1.Text);
cmd.Parameters.Add("@PostalCode", SqlDbType.Int).Value = Convert.ToInt32(PostalCodeTextBox.Text);
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = (EmailTextBox1.Text);
cmd.Parameters.Add("@HomeAddress", SqlDbType.NVarChar).Value = (AddressTextBox.Text);
trans.Commit();
cmd.ExecuteScalar();
}
catch (SqlException se)
{
try
{
trans.Rollback();
}
catch { Page.Title = "SqlException:" + se.Message; }
try
{
trans.Rollback();
}
catch (Exception ex)
{
Page.Title = "Exception:" + ex.Message;
}
}
finally
{
conn.Close();
}
}
but in debugging and tracing the code when it reaches to SerialNumber(which is Int in DB)this error occures :
FormatException was unhandled by user code
Input string was not in a correct format.