Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
        }
    }
}
Posted
Updated 27-Dec-13 8:12am
v2
Comments
CHill60 27-Dec-13 14:18pm    
Firstly consider using SQL Parameters instead of just concatenating text box contents directly into your sql queries - http://www.dotnetperls.com/sqlparameter[^].
You've said no error is shown, are you sure that you do end up in the finally clause?
What are the contents of TextBox1, 2 and 3?
vijay2013 28-Dec-13 12:51pm    
textbox1=name 2=rollNo 3=marks what happens is after running my main page is displayed after entering values and submit it data disappears and value is not in db.please give me a better solution my friend
ZurdoDev 27-Dec-13 14:23pm    
Put a breakpoint and make sure it is running.
vijay2013 2-Jan-14 12:34pm    
THIS IS THE ERROR IM GETTING WHEN use BREAKPOINT
//Split out for localization.
var L_GOBACK_TEXT = "Go back to the previous page.";
var L_REFRESH_TEXT = "Refresh the page.";
var L_MOREINFO_TEXT = "More information";
var L_OFFLINE_USERS_TEXT = "For offline users";
var L_RELOAD_TEXT = "Retype the address.";
var L_HIDE_HOTKEYS_TEXT = "Hide tab shortcuts";
var L_SHOW_HOTKEYS_TEXT = "Show more tab shortcuts";
var L_CONNECTION_OFF_TEXT = "You are not connected to the Internet. Check your Internet connection.";
var L_CONNECTION_ON_TEXT = "It appears you are connected to the Internet, but you might want to try to reconnect to the Internet.";

//used by invalidcert.js
var L_CertUnknownCA_TEXT = "The security certificate presented by this website was not issued by a trusted certificate authority.";
var L_CertExpired_TEXT = "The security certificate presented by this website has expired or is not yet valid.";
var L_CertCNMismatch_TEXT = "The security certificate presented by this website was issued for a different website's address.";
var L_CertRevoked_TEXT = "This organization's certificate has been revoked.";

var L_PhishingThreat_TEXT = "Phishing threat: This is a phishing website that impersonates a trusted website to trick you into revealing personal or financial information.";
var L_MalwareThreat_TEXT = "Malicious software threat: This site contains links to viruses or other software programs that can reveal personal information stored or typed on your computer to malicious persons.";

var L_ACR_Title_TEXT = "We were unable to return you to %s.";
var L_ACR_TitleFallback_TEXT = "We were unable to return you to the page you were viewing.";
var L_ACR_ReturnTo_TEXT = "Try to return to %s";
var L_ACR_ReturnToFallback_TEXT = "Try to return to the page you were viewing";
var L_ACR_GoHome_TEXT = "Go to your home page";
ZurdoDev 2-Jan-14 12:40pm    
That's not an error.

1 solution

You never commit your transaction. Try putting a call to the transaction's Commit() method at the end of the code in your try block. Like so...
C#
try{
    // command code
    // ...

    // commit transaction
    trans.Commit();

    Response.Write("<script>alert('done')</script>");
}
catch(Exception ex){
    // ... 
}
finally{
    // ...
}

Link to MS help page with an example...
http://msdn.microsoft.com/en-us/library/86773566(v=vs.110).aspx[^]
 
Share this answer
 
Comments
♥…ЯҠ…♥ 28-Dec-13 5:10am    
Good catch... 5'ed
idenizeni 30-Dec-13 11:41am    
Thanks.
vijay2013 28-Dec-13 13:20pm    
NO changes same problem exists

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