Cassie Mod

Cassie Mod

  • 1.6k
  • 488
  • 50.4k

The transaction is completed and cannot be used again.

Mar 31 2016 10:08 AM
hi,
 
ive got the following error, and I don't know how to fix it. I preformed a executeNonquery wich seems to work however when I commit the transaction  is says the transaction is already completed and cannot be used again. How can I fix this ???
 
  1. if (_files.Count != 0)  
  2.                 {  
  3.                     using (var transaction = connection.BeginTransaction())  
  4.                     {  
  5.                         try  
  6.                         {  
  7.                             UploadFiles(connection, transaction);  
  8.                             transaction.Commit();  // here it crashed
  9.                         }  
  10.                         catch (Exception ex)  
  11.                         {  
  12.                             transaction.Rollback();  
  13.                             Log.WriteException(ex);  
  14.                             throw;  
  15.                         }  
  16.                     }  
  17.                     var projatt2 = new ProjectAttachment();  
  18.                     AttachmentsGridView1.DataSource = projatt2.getProjectAttachmentsByProjectId(connection, null, ViewState["projectID"]);  
  19.                     AttachmentsGridView1.DataBind();  
  20.                 }  
  1. private void UploadFiles(SqlConnection connection, SqlTransaction transaction)  
  2.        {  
  3.            // upload and save files  
  4.            foreach (string key in _files.Keys)  
  5.            {  
  6.                var file = _files[key];  
  7.                if (string.IsNullOrEmpty(file?.FileName)) continue;  
  8.                Proj.getProjectById(connection, transaction, ViewState["projectID"]);  
  9.                var org = ProvisioningOrganisation.getOrganisationById(connection, transaction, Proj.PartnerID);  
  10.                var orgDir = Server.MapPath("../upload/" + org.UDF);  
  11.                if (!Directory.Exists(orgDir))  
  12.                    Directory.CreateDirectory(orgDir);  
  13.   
  14.                var newFileName = Path.GetFileName(file.FileName);  
  15.                var saveDir = Server.MapPath("../upload/" + org.UDF + "/" + newFileName);  
  16.                var tempSaveDir = saveDir;  
  17.                var exists = File.Exists(saveDir);  
  18.                var i = 1;  
  19.                while (exists)  
  20.                {  
  21.                    var filename = Path.GetFileNameWithoutExtension(saveDir);  
  22.                    var extension = Path.GetExtension(saveDir);  
  23.                    newFileName = filename + i + extension;  
  24.                    tempSaveDir = Server.MapPath("../upload/" + org.UDF + "/" + newFileName);  
  25.                    exists = File.Exists(tempSaveDir);  
  26.                    i++;  
  27.                }  
  28.                saveDir = tempSaveDir;  
  29.   
  30.                file.SaveAs(saveDir);  
  31.   
  32.                // save attachment  
  33.                var security = new SecurityLayer();  
  34.                Projatt.FileName = newFileName;  
  35.                Projatt.ProjectID = Proj.ID;  
  36.                Projatt.PermissionMediatorID = security.getpermissionMediatorID(connection, transaction);  
  37.                var projAttID = Projatt.saveProjectAttachment(connection, transaction);  
  38.                  
  39.                // Set binary data of attachment to database  
  40.                ProjectAttachmentLogic.saveBinaryDataAttachment(connection, transaction, tempSaveDir, projAttID);  
  41.            }  
  42.        }  
 
 
 
  1. public class ProjectAttachmentLogic  
  2.    {  
  3.   
  4.        public static void saveBinaryDataAttachment(SqlConnection connection, SqlTransaction transaction, string ProjattFilename, string ProjattId)  
  5.        {  
  6.            var id = Int32.Parse(ProjattId);  
  7.            byte[] file;  
  8.            using (var stream = new FileStream(ProjattFilename, FileMode.Open, FileAccess.Read))  
  9.            {  
  10.                using (var reader = new BinaryReader(stream))  
  11.                {  
  12.                    file = reader.ReadBytes((int)stream.Length);  
  13.                }  
  14.                ProjectAttachmentDal.InsertBinaryDataAttachment(connection, transaction, id, file);  
  15.            }  
    1. public class ProjectAttachmentDal  
    2. {  
    3.     /// <summary>  
    4.     /// Insert binary data of Attachment  
    5.     /// </summary>  
    6.     public static void InsertBinaryDataAttachment(SqlConnection connection, SqlTransaction transaction, int id, byte[] file)  
    7.     {  
    8.         using (connection)  
    9.         {  
    10.             using (var sqlCommand = new SqlCommand("INSERT INTO proj.BinaryDataAttachments (ID, Data) Values(@Id, @Datafile)", connection))  
    11.             {  
    12.                 sqlCommand.Transaction = transaction;  
    13.                 sqlCommand.Parameters.Add("@ID", SqlDbType.Int, id).Value = id;  
    14.                 sqlCommand.Parameters.Add("@Datafile", SqlDbType.VarBinary, file.Length).Value = file;  
    15.                 sqlCommand.ExecuteNonQuery();  
    16.             }  
    17.         }  
    18.     }   
    19. }  


Answers (1)