Question

I am using transaction to add diffrent database tables. When ı add Response.redirect(""); I am getting the error : SQLTransaction complete; is no longer available. If ı delete the response.redirect, It is working fine without the error.

protected void btnEkle_Click(object sender, EventArgs e)
        {
            SqlConnection baglanti = system.baglan();
            SqlCommand Trans_isl1 = new SqlCommand("Insert TblDetay (Ad,Yerler,Ulasim,Resim,Durum) values(@Ad,@Yerler,@Ulasim,@Resim,@Durum)", baglanti);

            Trans_isl1.Parameters.Add("@Ad", "deneme");
            Trans_isl1.Parameters.Add("@Yerler", "sdfgjkj");
            Trans_isl1.Parameters.Add("@Ulasim", "ads1");
            Trans_isl1.Parameters.Add("@Resim", "1.jpg");
            Trans_isl1.Parameters.Add("@Durum", true);

            SqlTransaction sqlTrans = baglanti.BeginTransaction(); // Transaction nesnesine işlemleri eşitliyoruz.
            Trans_isl1.Transaction = sqlTrans; 
            try
            {  
                 Trans_isl1.ExecuteNonQuery();
                 sqlTrans.Commit();
                 Response.Redirect("second.aspx?id="+ Session["id"]);
            }

            catch (Exception hata)
            { 
                Response.Write("İşleminiz yapılamadı, Oluşan Hatanın Detayı<br />" + hata);
                sqlTrans.Rollback();  
            }

            finally
            {  
                baglanti.Close();
                baglanti.Dispose();
                Trans_isl1.Dispose(); 
            }



        }
Was it helpful?

Solution

Response.Redirect calls Response.End, which will throw a ThreadAbortException, which is caught by your catch block and then the Rollback cannot occur because the transaction has already been committed. You can use Server.Transfer instead or use overload of Response.Redirect that allows you to specify whether Response.End is called.

See this link for more info http://support.microsoft.com/kb/312629

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top