Answered by:
This SqlTransaction has completed; it is no longer usable.” why am i getting this error

Question
-
User991471190 posted
Why am i getting System.InvalidOperationException: 'This SqlTransaction has completed; it is no longer usable.'??
There is no error in the code they why then is it running code inside catch block?
string s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected string GetValue(ListItemCollection list, int[] a)
{
string value = "";
for (int i = 0; i < a.Length; i++)
{
value += list[a[i]].Text.Trim() +",";
}return value;
}
protected bool Check(int[] a){
if (a.Length > 0){
return true;}
else {
return false;
}protected void Button1_Click(object sender, EventArgs e)
{ using (SqlConnection con = new SqlConnection(s)){
int row = 0;
int a = 0;
con.Open();
using (SqlCommand com = new SqlCommand("select * from UserData where Email='" + TextBox12.Text + "'", con))
{
using (SqlDataReader dr = com.ExecuteReader())
{
if (dr.HasRows)
{
Label7.Text = "Email Already Exists"+" ";
row++;
} } }
if (row == 0)
{
SqlTransaction t = con.BeginTransaction();try
{using (SqlCommand com = new SqlCommand("insert into UserData values(@Email,@Password,@UserName,@Number,@ClassOwner)", con))
{
com.Transaction = t;
com.Parameters.AddWithValue("@Email", TextBox12.Text);
com.Parameters.AddWithValue("@Password", TextBox8.Text);
com.Parameters.AddWithValue("@UserName", TextBox11.Text);
com.Parameters.AddWithValue("@Number", TextBox10.Text);
com.Parameters.AddWithValue("@ClassOwner", 1);
com.ExecuteNonQuery();
}
using (SqlCommand com = new SqlCommand("insert into ClassData values(@Email,@ClassName,@City,@Area,@FullAddress,@WebSite,@NumberOfBranch)", con))
{
com.Transaction = t;
com.Parameters.AddWithValue("@Email", TextBox12.Text);
com.Parameters.AddWithValue("@ClassName", TextBox1.Text);
com.Parameters.AddWithValue("@City", DropDownList1.SelectedItem.Text);
com.Parameters.AddWithValue("@Area", DropDownList2.SelectedItem.Text);
com.Parameters.AddWithValue("@FullAddress", TextBox5.Text);
com.Parameters.AddWithValue("@WebSite", TextBox7.Text);
com.Parameters.AddWithValue("@NumberOfBranch", 0);
com.ExecuteNonQuery();
}
if (Check(ListBox4.GetSelectedIndices()) || Check(ListBox2.GetSelectedIndices()) || Check(ListBox3.GetSelectedIndices()) || Check(ListBox5.GetSelectedIndices()) || Check(ListBox11.GetSelectedIndices()) || Check(ListBox12.GetSelectedIndices()) || Check(ListBox60.GetSelectedIndices()) || Check(ListBox6.GetSelectedIndices()) || Check(ListBox7.GetSelectedIndices()) || Check(ListBox8.GetSelectedIndices()) || Check(ListBox61.GetSelectedIndices()))
{using (SqlCommand com = new SqlCommand("insert into Education1 values(@Email,@ClassName,@Primary,@Medical,@Enginnering,@Law,@Science,@Commerce,@Arts,@Architecht)", con))
{
com.Transaction = t;
string prime = GetValue(ListBox4.Items, ListBox4.GetSelectedIndices()) + GetValue(ListBox2.Items, ListBox2.GetSelectedIndices()) + GetValue(ListBox3.Items, ListBox3.GetSelectedIndices()) + GetValue(ListBox5.Items, ListBox5.GetSelectedIndices());
com.Parameters.AddWithValue("@Email", TextBox12.Text);
com.Parameters.AddWithValue("@ClassName", TextBox1.Text);
com.Parameters.AddWithValue("@Primary", prime);
com.Parameters.AddWithValue("@Medical", GetValue(ListBox11.Items, ListBox11.GetSelectedIndices()));
com.Parameters.AddWithValue("@Enginnering", GetValue(ListBox12.Items, ListBox12.GetSelectedIndices()));
com.Parameters.AddWithValue("@Law", GetValue(ListBox60.Items, ListBox60.GetSelectedIndices()));
com.Parameters.AddWithValue("@Science", GetValue(ListBox6.Items, ListBox6.GetSelectedIndices()));
com.Parameters.AddWithValue("@Commerce", GetValue(ListBox7.Items, ListBox7.GetSelectedIndices()));
com.Parameters.AddWithValue("@Arts", GetValue(ListBox8.Items, ListBox8.GetSelectedIndices()));
com.Parameters.AddWithValue("@Architecht", GetValue(ListBox61.Items, ListBox61.GetSelectedIndices()));
com.ExecuteNonQuery();
}
}
if (Check(ListBox63.GetSelectedIndices()) || Check(ListBox52.GetSelectedIndices()) || Check(ListBox36.GetSelectedIndices()) || Check(ListBox53.GetSelectedIndices()) || Check(ListBox49.GetSelectedIndices()))
{using (SqlCommand com = new SqlCommand("insert into Education2 values(@Email,@ClassName,@CivilAviation,@Radio,@Acting,@Multimedia,@Vocational)", con))
{
com.Transaction = t;
com.Parameters.AddWithValue("@Email", TextBox12.Text);
com.Parameters.AddWithValue("@ClassName", TextBox1.Text);
com.Parameters.AddWithValue("@CivilAviation", GetValue(ListBox63.Items, ListBox63.GetSelectedIndices()));
com.Parameters.AddWithValue("@Radio", GetValue(ListBox52.Items, ListBox52.GetSelectedIndices()));
com.Parameters.AddWithValue("@Acting", GetValue(ListBox36.Items, ListBox36.GetSelectedIndices()));
com.Parameters.AddWithValue("@Multimedia", GetValue(ListBox53.Items, ListBox53.GetSelectedIndices()));
com.Parameters.AddWithValue("@Vocational", GetValue(ListBox49.Items, ListBox49.GetSelectedIndices()));
com.ExecuteNonQuery();}
}
if (Check(ListBox37.GetSelectedIndices()) || Check(ListBox64.GetSelectedIndices()) || Check(ListBox65.GetSelectedIndices()))
{
using (SqlCommand com = new SqlCommand("insert into Others values(@Email,@ClassName,@Dance,@Sport,@Music)", con))
{
com.Transaction = t;
com.Parameters.AddWithValue("@Email", TextBox12.Text);
com.Parameters.AddWithValue("@ClassName", TextBox1.Text);
com.Parameters.AddWithValue("@Dance", GetValue(ListBox37.Items, ListBox37.GetSelectedIndices()));
com.Parameters.AddWithValue("@Sport", GetValue(ListBox64.Items, ListBox64.GetSelectedIndices()));
com.Parameters.AddWithValue("@Music", GetValue(ListBox65.Items, ListBox65.GetSelectedIndices()));
com.ExecuteNonQuery();}
}
HttpCookie cookie = new HttpCookie("MyEmail");
cookie["MyEmail"] = TextBox12.Text;
cookie["MyName"] = TextBox11.Text;
cookie.Expires = DateTime.Now.AddHours(24);
Response.Cookies.Add(cookie);
t.Commit();
Response.Redirect("Home2.aspx");}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (con.State == ConnectionState.Open)
{
con.Close();
}
t.Rollback();
}}}}Sunday, January 6, 2019 3:17 PM
Answers
-
User991471190 posted
Apparently when i set response.redirect("Home2.aspx",false) it dosent go to catch block
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, January 6, 2019 3:37 PM