Re: Place of BeginTransaction in code

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Declare the transaction object outside the try/catch. Just don't set it to
anything. Then, after you open the connection, inside the try block, set
the variable to the new transaction.

There is no law that says you have to declare the variable and set it to
something in the same line. You can declare everything at the top of your
method, and then use it later. In fact, that is my personal preference,
just to see all the variables involved in my method, so I don't have to
track them down throughout the method.

"Mukesh" <cmukesh19@xxxxxxxxx> wrote in message
news:1160485739.992551.62380@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,
Consider this code

SqlConnection oConn = new
SqlConnection(AccountsConnectionString);
SqlCommand cmdInsert = new SqlCommand("UpdateQuery");
//Location 1:
//SqlTransaction sqlTran = oConn.BeginTransaction();
try
{
oConn.Open();
//Location 2:
//SqlTransaction sqlTran = oConn.BeginTransaction();
cmdInsert.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
//Location 3:
sqlTran.Rollback();
}
finally
{
oConn.Close();
}

I have to start a transaction and commit or rollback depending upon the
condition of success of command execution. The problem is if I write
code to Begin Transaction at location 1 it won't work as connection is
still closed. If I write the same at location 2, I won't be able to
rollback transaction at location 3 as sqlTran object is out of
referance. What should I do? I am using framework 2.0.

Thanks and Regards
Chakravarti Mukesh



.



Relevant Pages

  • Re: Place of BeginTransaction in code
    ... you are assuming you have a transaction. ... connection could not be opened, no transaction would ever have been started. ... There is no law that says you have to declare the variable and set it to ... I have to start a transaction and commit or rollback depending upon the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ask for standard sp writing
    ... transaction creation. ... > -- Declare the variables to store the values returned by FETCH. ... > OPEN SampleCrsr ...
    (microsoft.public.sqlserver.programming)
  • rollback transaction not rolling back
    ... the insert statements fail. ... It isn't rolling back the transaction if the ... @Instrument_ID smallint, ... Declare @Spot Smallint ...
    (microsoft.public.sqlserver.programming)
  • Re: Place of BeginTransaction in code
    ... Then, after you open the connection, inside the try block, set ... There is no law that says you have to declare the variable and set it to ... I have to start a transaction and commit or rollback depending upon the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Rollback timeout ???
    ... I actually like and would recommend using the tsql Begin Transaction instead ... the ammount of time the connection remains in the pool. ... As to setting the transaction timeout period for the BeginTransaction API... ... >> create a transaction object, ...
    (microsoft.public.dotnet.framework.adonet)