Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Can anyone please help me..

im performing insert update and Delete within a transaction and i want to use Select Command.
It is possible to select data with in a Transaction?

My Code--

C#
SqlConnection Con = new SqlConnection(connectionStr);
        Con.Open();
        SqlTransaction Trans = Con.BeginTransaction();
        try
        {
            CommonMethods.ExecuteNonQueryWithTrans("Insert into tbldata(Name) Values('" + TextBox1.Text.Trim() + "')", Con, Trans);
            CommonMethods.ExecuteNonQueryWithTrans("Update tbldata set Name='Surendra'", Con, Trans);
           // CommonMethods.ExecuteNonQueryWithTrans("Delete From tbldata", Con, Trans); //*Generating Error*//
            CommonMethods.LoadGridData(GridView1,"Select * from tbldata");
            Trans.Commit();
            Response.Write("<font color='green'>Transaction Committed Successfully!!</font>");
        }
        catch (Exception ExX)
        {
            Trans.Rollback();
            Con.Close();
            Response.Write("<font color='Red'>Transaction rollbacked due to error!!</font>");
        }


If i Use this Like below code im able to use select Command because Transaction is Commited now
and Second option is With Nolock

C#
SqlConnection Con = new SqlConnection(connectionStr);
       Con.Open();
       SqlTransaction Trans = Con.BeginTransaction();
       try
       {
           CommonMethods.ExecuteNonQueryWithTrans("Insert into tbldata(Name) Values('" + TextBox1.Text.Trim() + "')", Con, Trans);
          // CommonMethods.ExecuteNonQueryWithTrans("Update tbldata set Name='Surendra'", Con, Trans);
          // CommonMethods.ExecuteNonQueryWithTrans("Delete From tbldata", Con, Trans); //*Generating Error*//
          // CommonMethods.LoadGridData(GridView1, "Select * from tbldata with (NOLOCK)");
           Trans.Commit();
           CommonMethods.LoadGridData(GridView1, "Select * from tbldata");
           Response.Write("<font color='green'><b>Transaction Committed Successfully!!</b></font>");
       }
       catch (Exception ExX)
       {
           Trans.Rollback();
           Con.Close();
           Response.Write("<font color='Red'><b>Transaction rollbacked due to error!!<b></font>");
       }
Posted
Updated 22-Aug-14 9:20am
v3
Comments
David Lee 145 22-Aug-14 14:48pm    
I think you need to consider transaction isolation level.
Surendra0x2 22-Aug-14 14:51pm    
How?Please Tell me Sir
PIEBALDconsult 22-Aug-14 15:01pm    
Of course you can, but we don't know what a CommonMethods is -- as far as I can tell it's a class you created and it may be causing you trouble.
Can you share the relevent code?
Surendra0x2 22-Aug-14 15:15pm    
Sir,Yes CommonMethod is a Class and Method inside class is-
public static void ExecuteNonQueryWithTrans(string Sql, SqlConnection sqlconn, SqlTransaction Sqltrans)
{

try
{

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlconn;
sqlCmd.Transaction = Sqltrans;
sqlCmd.CommandText = Sql;
sqlCmd.CommandTimeout = 0;
sqlCmd.ExecuteNonQuery();

}
catch (SqlException ex)
{
throw new ApplicationException("Data error (Non Query).");
}

}
PIEBALDconsult 22-Aug-14 15:21pm    
Other than swallowing the original Exception (which might help you to figure out what's going wrong), that looks OK.
What problem are you having?

1 solution

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