1

Ok so i have a webform and 5 FileUpload control..a user can upload any number of files from 1 to 5 but if anyone of these files does not get uploaded then I want to rollback everything... For ex: if user has selected 4 files and if something unexpected occurs at 4th then I want to remove or rollback all the previous 3 file uploads.. I tried this..

 try
{
  using (TransactionScope scope = new TransactionScope())
                {
dboperation dbinsert=new dboperation();
if (file1.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}

if (file2.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}

if (file3.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}//till ...file5

scope.Complete();
}//end of transactionscope
}


catch { }

'dboperation' is a class in c# file and 'dbinsert' is a method which is executing an insert stored procedure. My guess is that I need to use Transaction Scope but I am not sure if I am correct and even if I am how am I supposed to achieve this?

Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

2 Answers2

2

You need to implement transaction. You should start the transaction before inserting first one and catch any errors that occur. in case of error you have to rollback the transaction. And if all goes well you can commit your transaction.

You should also move you connection outside the dboperation or make a method in dboperation that takes connection from outside and uses that

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • i have already tried that but it did not work.. may be i am implementing it the wrong way.. see my code i have updated it. – SamuraiJack Aug 27 '13 at 10:04
  • in catch you need to rollback your transaction. – Ehsan Aug 27 '13 at 10:06
  • How? and is it not supposed to roll back itself in case there is an error? why is it getting aborted in the first place when there is no reason for it to abort. – SamuraiJack Aug 27 '13 at 10:07
  • is this any useful.. Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool. – SamuraiJack Aug 27 '13 at 10:22
  • Like the error message states, you need to enable the MSDTC service on the sql server machine to use TransactionScope transactions... – bastos.sergio Aug 27 '13 at 10:22
  • @bastos.sergio i am using single Database in my project.. but in this particular case there are two different tables and two different stored procedures involved but they are all working on same database.. then how is it Distributed Transaction because as far as i know (and i can be wrong) that Distributed Transaction occurs when there are more than one database servers involved. – SamuraiJack Aug 27 '13 at 10:30
  • 1
    You're opening more than one connection to the DB... Please check this question [http://stackoverflow.com/questions/7950054/using-transactionscope-system-transactions-transactionabortedexception-the-tr](using TransactionScope) – bastos.sergio Aug 27 '13 at 10:36
  • Thank you , i am trying to figure out how to make this work without opening and closing the connection again and again. – SamuraiJack Aug 27 '13 at 11:12
  • move you connection outside the dboperation or make a method in dboperation that takes connection from outside and uses that. – Ehsan Aug 27 '13 at 11:15
  • @NoOne do i have to mention roll back explicitly? – SamuraiJack Aug 27 '13 at 12:26
  • @NoOne include `"move you connection outside the dboperation or make a method in dboperation that takes connection from outside and uses that."` in your post so i shall mark it as an answer :) – SamuraiJack Aug 28 '13 at 05:23
1

for this you need to use Transaction something like this. I give you example.

class WithTransaction
{
    public WithTransaction()
    {
        string FirstQuery = "INSERT INTO Table1 VALUES('Vineeth',24)";
        string SecondQuery = "INSERT INTO Table2 VALUES('HisAddress')";
        int ErrorVar = 0;
        using (SqlConnection con = new SqlConnection("your connection string"))
        {
            try
            {
                SqlCommand ObjCommand = new SqlCommand(FirstQuery, con);
                SqlTransaction trans;
                con.Open();
                trans = con.BeginTransaction();
                ObjCommand.Transaction = trans;
                //Executing first query

                //What ever operation on your database do here

                ObjCommand.ExecuteNonQuery();  //Exected first query
                ObjCommand.CommandText = SecondQuery;
                ObjCommand.ExecuteNonQuery();  //Exected first query
                //Everything gone fine. So commiting
                ObjCommand.Transaction.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error but we are rollbacking");
                ObjCommand.Transaction.Rollback();
            }
            con.Close();
        }
    }
}

Or you can use TransactionScope

check this Link

TransactionScope

I hope this will help you.

AB Vyas
  • 2,349
  • 6
  • 26
  • 43
  • Thanks for your answer but my situation is not as simple as that.. i have implement the transaction on the code i have mentioned in question. – SamuraiJack Aug 27 '13 at 11:13
  • you can implement that code in here also. you have to pass your transaction to your class. write you code in try catch block of my code where i put comment. please have a look. – AB Vyas Aug 27 '13 at 11:20
  • Or you can use transaction scope also. check this post http://www.dotnetfunda.com/articles/article70.aspx http://codingcramp.blogspot.com.au/2009/06/how-to-setup-and-use-transactionscope.html – AB Vyas Aug 27 '13 at 11:26