public partial class _1stex : System.Web.UI.Page { SqlConnection conn; SqlCommand cmd; SqlTransaction trans; protected void Page_Load(object sender, EventArgs e) { conn = new SqlConnection(ConfigurationManager.AppSettings["MasterConn"].ToString()); } protected void Button1_Click(object sender, EventArgs e) { conn.Open(); trans = conn.BeginTransaction(); cmd = new SqlCommand(); cmd.Connection = conn; cmd.Transaction = trans; try { cmd.CommandText="INSERT INTO TRANS1 VALUES("+TextBox1.Text+",'"+TextBox2.Text+"','"+TextBox3.Text+"')"; cmd.ExecuteNonQuery(); cmd.CommandText ="INSERT INTO TRANS2 VALUES(" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "')"; cmd.ExecuteNonQuery(); cmd.CommandText ="INSERT INTO TRANS3 VALUES(" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "')"; cmd.ExecuteNonQuery(); Response.Write("<script>alert('done')</script>"); } catch(Exception ex) { trans.Rollback(); Response.Write("<script>alert('"+ex.Message+"')</script>"); } finally { conn.Close(); TextBox1.Text = TextBox2.Text = TextBox3.Text = string.Empty; } } }
finally
try{ // command code // ... // commit transaction trans.Commit(); Response.Write("<script>alert('done')</script>"); } catch(Exception ex){ // ... } finally{ // ... }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)