Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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;
        //if (con.State == ConnectionState.Open)
        //{
        //    con.Close();
        //}
        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();//error occures here***********
            LabelMessage.Text = exp.Message;
        }
        finally
        {
           // trans.Dispose();
        }
    }



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]
Posted
Updated 26-Mar-11 20:19pm
v3

1 solution

If you are getting an exception in your catch block, which says that the SqlTransaction has completed, then it is likely that that the the original error is coming after the Commit. I would suggest that you nest catch blocks:
C#
try
{
    try
        {     
        cmd = new SqlCommand("Insert into Chemist(ChemistName,Qualification,Birthdate,Email,Website
...
        cmd.ExecuteNonQuery();
        trans.Commit();
        con.Close();
        }
    catch (Exception exp)
        {
        trans.Rollback();
        LabelMessage.Text = exp.Message;
        }
    LabelCode.Text = ClassChemist.getChemistCode(TextBoxName.Text).ToString();
    LabelMessage.Text = "Chemist Saved Successfully";
    }
catch (Exception expOuter)
    {
    LabelMessage.Text = expOuter.Message;    
    }
}


You might also want to look at using Close and Dispose on your SqlConnection and SqlCommand objects as well as the SqlTransaction.
 
Share this answer
 
Comments
nitin bhoyate 27-Mar-11 7:22am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900