I am Developing one application.
I want to store the names of chemist in database
The code is as follows
if (TextBoxName.Text == "")
{
LabelMessage.Text = "Please Enter Chemist name";
}
else if( ClassValidation.checkExist("Chemist","ChemistName",TextBoxName.Text )==true)
{
LabelMessage.Text = "Chemist Name Already Exist";
}
else
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MRProjectConnectionString"].ConnectionString);
SqlCommand cmd;
con.Open();
SqlTransaction trans;
trans = con.BeginTransaction(IsolationLevel.Serializable);
try
{
cmd = new SqlCommand("Insert into Chemist(ChemistName,Qualification,Birthdate,Email,Website ,MobNo,HqName,Anniversary,Hobbies,other ,Submittedby,Status)Values(@ChemistName,@Qualification,@Birthdate,@Email,@Website ,@MobNo,@HqName,@Anniversary,@Hobbies,@other ,@Submittedby,@Status)", con, trans);
cmd.Parameters.Add("@ChemistName", SqlDbType.NVarChar).Value = TextBoxName.Text;
cmd.Parameters.Add("@Qualification", SqlDbType.NVarChar).Value = TextBoxQualification.Text;
cmd.Parameters.Add("@Birthdate", SqlDbType.SmallDateTime).Value = TextBoxBirthdate.Text;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = TextBoxEmail.Text;
cmd.Parameters.Add("@Website", SqlDbType.NVarChar).Value = TextBoxWebsite.Text;
cmd.Parameters.Add("@MobNo", SqlDbType.NVarChar).Value = TextBoxMobileNo.Text;
cmd.Parameters.Add("@HqName", SqlDbType.NVarChar).Value = DropDownListHqName.Text;
cmd.Parameters.Add("@Anniversary", SqlDbType.SmallDateTime).Value = TextBoxAnniversary.Text;
cmd.Parameters.Add("@Hobbies", SqlDbType.NVarChar).Value = TextBoxHobbies.Text;
cmd.Parameters.Add("@other", SqlDbType.NVarChar).Value = TextBoxOther.Text;
cmd.Parameters.Add("@Submittedby", SqlDbType.NVarChar).Value = "1";
cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "P";
cmd.ExecuteNonQuery();
trans.Commit();
con.Close();
LabelCode.Text = ClassChemist.getChemistCode(TextBoxName.Text).ToString();
LabelMessage.Text = "Chemist Saved Successfully";
}
catch (Exception exp)
{
trans.Rollback();
LabelMessage.Text = exp.Message;
}
finally
{
}
}
Problems:
If i enter all values correct then code works fine but when i enter some wrong values such as wrong date format It gives error message as i am expecting ,
But after getting exception (error message ) if i corrects the data data is saved successfully but one Exception is thrown
This SqlTransaction has completed; it is no longer usable
in catch block..
Please help me.
[edit]Code block added - OriginalGriff[/edit]