Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Ok so i have a webform and 5 FileUpload control..a user can upoad any number of files from 1 to 5 but if anyone of the 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..

dboperation dbinsert=new dboperation();
C#
if (file1.ContentLength > 0)
{
      .......
      .......
dbo.insert(bytes, lastid, file2.FileName);
}


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


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

dboperation is a class in c# file and dbinsert is a method which is executing an insert stored procedure.
Posted

Use SqlTransaction Object in the beginning of Connection open
and pass that transaction object in each SqlCommand object.

And by using Commit() or Rollback() Method you can decide the whether to save the changes or not.


C#
using (var Conn = new SqlConnection(_ConnectionString))
{
    SqlTransaction trans = null;
    try
    {
        Conn.Open();
        trans = Conn.BeginTransaction();

        using (SqlCommand Com = new SqlCommand(ComText, Conn, trans))
        {
            /* DB work */
        }
        trans.Commit();
    }
    catch (Exception Ex)
    {
        if (trans != null) trans.Rollback();
        return -1;
    }
}
 
Share this answer
 

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