Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,
Pl see my code. Is it wright Sql & C# Transcation code? Pl give the advice for more fine tune code.

C#
private void btnsave_Click(object sender, EventArgs e)
        {
            string sq = null;
            try
            {
                sq = "Insert into unpw (username,password)values(@username,@password)";
                cmd = new System.Data.SqlClient.SqlCommand(sq, con);


                Trans = con.BeginTransaction();
                    cmd.Parameters.AddWithValue("@username", txtun.Text);
                    cmd.Parameters.AddWithValue("@password", txtpwd.Text);
                    cmd.Transaction = Trans;
                    cmd.ExecuteNonQuery();
                    Trans.Commit();

                    sq = "Insert into unpw1 (username,password)values(@username,@password)";
                    cmd = new System.Data.SqlClient.SqlCommand(sq, con);
                    cmd.Parameters.AddWithValue("@UserName", txtun.Text);
                    cmd.Parameters.AddWithValue("@password", txtpwd.Text);
                    cmd.Transaction = Trans;
                    cmd.ExecuteNonQuery();
                   //Trans.Commit();
                    MessageBox.Show("Save");
                    cmd.Dispose();
                    con.Close();


            }
            catch (Exception ex)
            {

                Trans.Rollback();

                MessageBox.Show(ex.Message);
            }


        }


Thanks & Advance
sreeni.
Posted
Updated 16-Jul-14 1:08am
v2
Comments
jo.him1988 16-Jul-14 3:50am    
if you disable //Trans.Commit(); record will not insert in Database and if you forgot somewhere to write cmd.Dispose();and con.Close(); will take your sql server to busy

so always best to open transaction commit it and if any error rollback it
so fst insertion is work fine
sreenivashan 16-Jul-14 3:55am    
okay i thankyou.

Change your code with the below changes. I assume that you have declare Trans variable somewhere in your code
C#
private void btnsave_Click(object sender, EventArgs e)
{
    string sq = null;
    try
    {
        Trans = con.BeginTransaction();
        sq = "Insert into unpw (username,password)values(@username,@password)";
        cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);


        cmd.Parameters.AddWithValue("@username", txtun.Text);
        cmd.Parameters.AddWithValue("@password", txtpwd.Text);
        cmd.Transaction = Trans;
        cmd.ExecuteNonQuery();


        sq = "Insert into unpw1 (username,password)values(@username,@password)";
        cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);
        cmd.Parameters.AddWithValue("@UserName", txtun.Text);
        cmd.Parameters.AddWithValue("@password", txtpwd.Text);
        cmd.Transaction = Trans;
        cmd.ExecuteNonQuery();
        Trans.Commit();
        MessageBox.Show("Save");
        cmd.Dispose();
        con.Close();


    }
    catch (Exception ex)
    {

        Trans.Rollback();

        MessageBox.Show(ex.Message);
    }


}
 
Share this answer
 
v2
Comments
sreenivashan 16-Jul-14 3:54am    
Thank You so much Madhu Nair. Hence How can I notice the rollback? If failed?
jo.him1988 16-Jul-14 4:38am    
your code is saying all things, its roll back if there is exception
when you got MessageBox.Show(ex.Message); you know that its rollback
and best way to show the message to user is MessageBox.Show("user creation failed"); or
MessageBox.Show("user creation failed"+ex.Message);
and as a programmer you can log your ex.Message
sreenivashan 16-Jul-14 4:57am    
Thank you Jo... I understand from your adivce. Thank you so much. I am happy with your replay.
agent_kruger 16-Jul-14 4:51am    
+5 vote, appropriate answer
protected void btnsave_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString);
       con.Open();
       SqlTransaction Trans =  con.BeginTransaction();
       string sq = null;
       try
       {
           sq = "Insert into unpw (username,password)values(@username,@password)";
           System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
           cmd.Parameters.AddWithValue("@username", txtun.Text);
           cmd.Parameters.AddWithValue("@password", txtpwd.Text);
           cmd.Transaction = Trans;
           int i= cmd.ExecuteNonQuery();
           sq = "Insert into unpw1 (username,password)values(@username,@password)";
           //cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
           //cmd.Parameters.AddWithValue("@UserName", txtun.Text);
           //cmd.Parameters.AddWithValue("@password", txtpwd.Text);
           //cmd.Transaction = Trans;
           cmd.CommandText = sq;
           cmd.ExecuteNonQuery();
           Trans.Commit();
           MessageBox.Show("Save");
           cmd.Dispose();
           con.Close();
       }
       catch (Exception ex)
       {
           Trans.Rollback();
           MessageBox.Show(ex.Message);
       }
   }


No need to write once again the following line (if the statement is same):
//cmd = new System.Data.SqlClient.SqlCommand(sq, con, Trans);
           //cmd.Parameters.AddWithValue("@UserName", txtun.Text);
           //cmd.Parameters.AddWithValue("@password", txtpwd.Text);
           //cmd.Transaction = Trans;
 
Share this answer
 
v3
Comments
sreenivashan 16-Jul-14 5:02am    
hi somendratiwari.. is it need "protected void btnsave_Click(object sender, EventArgs e)" or private void btnsave_Click(object sender, EventArgs e)?
somendratiwari 16-Jul-14 5:13am    
it should be "protected void btnsave_Click(object sender, EventArgs e) " because of inaccessibility.

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