Forums - C# Corner

Forum guidelines
Cassie Mod

Cassie Mod

  • 1.1k
  • 428
  • 13.2k

The timeout period has expired before the operation was comp

Apr 4 2016 8:31 AM
Hi I get the following error, but I don't know how to fix it. ( still a bit of the same error that I had earlier.. the error message  is: Timeout has expired. The timeout period has expired before the operation was completed or the server is not responding. the idea was to convert a file into binay data and upload it to an SQL database.
 
I get this when I do a transaction.commit in the page_load method. However when I do this in the ProjectAttachmentDal.InsertBinaryDataAttachment is doesn't gives an error ( only it shouldn't be in the dal everyone says so. here is my code
 
thnx guys 
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.IO;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using OneXS.Framework.Portal.Website;  
  10. using OneXS.Orion.BackOffice.Billing.ServiceAgent;  
  11. using OneXS.Orion.DatabaseClasses;  
  12. using OneXS.Orion.FrontOffice.Sales.BusinessLogic;  
  13. using OneXS.Orion.FrontOffice.Sales.BusinessLogic.Billing;  
  14. using OneXS.Orion.FrontOffice.Sales.DataAccess;  
  15. using OneXS.Orion.FrontOffice.Sales.Model;  
  16. using OneXS.Orion.HelperClasses;  
  17. using OneXS.Orion.Logging;  
  18. using OneXS.Orion.Properties;  
  19. using OneXS.Orion.FrontOffice.Sales.Core;  
  20.   
  21. namespace OneXS.Orion.WebSite.Forms.Project  
  22. {  
  23.     public partial class Project : OrionSalesPage  
  24.     {  
  25.         protected DatabaseClasses.Project Proj = new DatabaseClasses.Project();  
  26.         protected ProjectAttachment Projatt = new ProjectAttachment();  
  27.         protected FrontOffice.Sales.Model.ProjectHosted ProjectHosted;  
  28.         protected Organisation Organisation;  
  29.         private HttpFileCollection _files;  
  30.         readonly LogManager _log = new LogManager();  
  31.   
  32.         protected new void Page_Load(object sender, EventArgs e)  
  33.         {  
  34.             try  
  35.             {  
  36.                 base.Page_Load(sender, e);  
  37.   
  38.                 using (var connection = DatabaseLayer.OpenConnection())  
  39.                 using (var transaction = connection.BeginTransaction())  
  40.                 {  
  41.                     {  
  42.                         _files = Request.Files;  
  43.                         if (string.IsNullOrEmpty(Request.Params["projectid"]))  
  44.                             Response.Redirect(PortalApplication.computeDocumentUrl(PortalCommands.Orion2NotFound));  
  45.   
  46.                         ViewState["projectID"] = Request.Params["projectid"];  
  47.                         ViewState["locationID"] = Request.Params["locationid"];  
  48.                         ViewState["isAllowedBusinessPro"] = "0";  
  49.   
  50.                         var projectRow = Proj.getProjectById(connection, transaction, Request.Params["projectid"]);  
  51.                         if (projectRow == null)  
  52.                             throw new Exception("Project niet gevonden.");  
  53.   
  54.                         CheckPageRight(Convert.ToInt32(Proj.PartnerID) != PortalApplication.User.OrgId ? OrionUserRights.ProjectenPartner : OrionUserRights.Projecten);  
  55.   
  56.                         if (string.IsNullOrEmpty(Request.Params["locationid"]))  
  57.                         {  
  58.                             var locations = ProvisioningLocation.getLocationsByProjectId(connection, transaction, Request.Params["projectid"]);  
  59.                             if (locations == null || locations.Rows.Count == 0)  
  60.                                 throw new Exception("Geen locaties voor het project gevonden.");  
  61.   
  62.                             HttpContext.Current.Response.Redirect(PortalApplication.computeDocumentUrl(PortalCommands.Orion2Project) + "?projectid=" + Request.Params["projectid"] + "&locationid=" + locations.Rows[0][0]);  
  63.                         }  
  64.   
  65.                         ProjectHosted = ProjectHostedDal.GetProjectHostedByProjectId(connection, transaction, int.Parse(Proj.ID));  
  66.                         Organisation = OrganisationDal.GetOrganisationById(connection, transaction, int.Parse(Proj.PartnerID));  
  67.   
  68.                         if (!IsPostBack)  
  69.                         {  
  70.                             FillProductTypeRadioButtonList(connection, transaction);  
  71.                             trStatus.Visible = PortalApplication.User.IsOneXSMedewerker;  
  72.                             var partner = ProvisioningOrganisation.getOrganisationById(connection, transaction, projectRow["PartnerID"]);  
  73.                             if (partner.OrgType != OrgTypeEnum.Reseller.ToString())  
  74.                             {  
  75.                                 pnlHardwareOptions1.Visible = false;  
  76.                                 pnlHardwareOptions2.Visible = false;  
  77.                                 trReadIsCPSProject.Visible = false;  
  78.                             }  
  79.   
  80.                             Looptijd.Items.Add("12");  
  81.                             Looptijd.Items.Add("24");  
  82.                             Looptijd.Items.Add("36");  
  83.                             Looptijd.Items.Add("48");  
  84.                             Looptijd.Items.Add("60");  
  85.                             trReadLooptijd.Visible = !Proj.isCPSProject;  
  86.                             trEditLooptijd.Visible = !Proj.isCPSProject;  
  87.                             pnlHardwareOptions1.Visible = !Proj.isCPSProject;  
  88.                             pnlHardwareOptions2.Visible = !Proj.isCPSProject;  
  89.                             trEditHasVPN.Visible = !Proj.isCPSProject && Proj.hasVPN;  
  90.   
  91.                             // set Partner contacts  
  92.                             contactPersoonPartner.Items.Clear();  
  93.                             contactPersoonPartner.Items.Add(new ListItem(KeyConstants.PLEASE_SELECT, string.Empty));  
  94.                             var orgContacts = ProvisioningContact.getProvisioningContactsByOrgId(connection, transaction, Proj.PartnerID);  
  95.                             if (orgContacts != null && orgContacts.Rows.Count > 0)  
  96.                                 foreach (DataRow orgContact in orgContacts.Rows)  
  97.                                     contactPersoonPartner.Items.Add(new ListItem(orgContact["Voornaam"] + " " + orgContact["Tussenvoegsel"] + "" + orgContact["Achternaam"], orgContact["ID"].ToString()));  
  98.   
  99.                             // set Customer contacts  
  100.                             contactPersoonCustomer.Items.Clear();  
  101.                             contactPersoonCustomer.Items.Add(new ListItem(KeyConstants.PLEASE_SELECT, string.Empty));  
  102.   
  103.                             var custContacts = ProvisioningContact.getProvisioningContactsByOrgId(connection, transaction, Proj.OrgID);  
  104.                             if (custContacts != null)  
  105.                                 foreach (DataRow custContact in custContacts.Rows)  
  106.                                     contactPersoonCustomer.Items.Add(new ListItem(custContact["Voornaam"] + " " + custContact["Tussenvoegsel"] + "" + custContact["Achternaam"], custContact["ID"].ToString()));  
  107.   
  108.                             PageHeader.InnerText = "P" + Proj.ProjectCode;  
  109.                             OneXSCode.Text = Proj.OneXSCode;  
  110.   
  111.                             var loggedInUserOrgId = PortalApplication.User.OrgId.ToString();  
  112.   
  113.                             if (!string.IsNullOrEmpty(loggedInUserOrgId))  
  114.                             {  
  115.                                 var loggedInUserOrg = ProvisioningOrganisation.getOrganisationById(connection, transaction, loggedInUserOrgId);  
  116.                                 if (!string.IsNullOrEmpty(loggedInUserOrg?.ID))  
  117.                                     ViewState["isAllowedBusinessPro"] = loggedInUserOrg.isAllowedBusinessPro ? "1" : "0";  
  118.                             }  
  119.                             chkInstallationPartner.Enabled = ProjectLogic.IsOneProProductType(ProjectHosted) && Organisation.isAllowedCustomerInstallation;  
  120.   
  121.                             ProjectCode.Text = $"P{Proj.ProjectCode}";  
  122.                             Looptijd.Text = Proj.Looptijd;  
  123.                             PartnerCode.Text = Proj.PartnerCode;  
  124.                             ResellerLevertHardware.Checked = Proj.ResellerLevertHardware == "1";  
  125.                             Status.Text = Proj.Status.ToString();  
  126.                             chkHasVPN.Checked = Proj.hasVPN;  
  127.                             chkInstallationPartner.Checked = Proj.InstallationByPartner;  
  128.                             chkHardwareServicePlus.Checked = Proj.HardwareServicePlus;  
  129.   
  130.                             var projatt = new ProjectAttachment();  
  131.                             AttachmentsGridView1.DataSource = projatt.getProjectAttachmentsByProjectId(connection, transaction, ViewState["projectID"]);  
  132.                             AttachmentsGridView1.DataBind();  
  133.   
  134.                             var canEditProject = CanEditProject(Proj.Status, Convert.ToInt32(Proj.PartnerID));  
  135.                             EditButton.Visible = canEditProject;  
  136.                             SaveButton.Visible = canEditProject;  
  137.   
  138.                             if (!string.IsNullOrEmpty(Request.Params["attachmentid"]) && _files.Count == 0)  
  139.                             {  
  140.                                 projatt.getProjectAttachmentById(connection, transaction, Request.Params["attachmentid"]);  
  141.   
  142.                                 if (File.Exists(Server.MapPath("../upload/" + partner.UDF + "/" + projatt.FileName)))  
  143.                                 {  
  144.                                     Response.Buffer = true;  
  145.                                     Response.Clear();  
  146.                                     Response.TransmitFile(Server.MapPath("../upload/" + partner.UDF + "/" + projatt.FileName));  
  147.                                     Response.AddHeader("Content-Disposition""Attachment; filename=" + HttpUtility.UrlEncode(projatt.FileName));  
  148.                                     Response.ContentType = "application/octet-stream";  
  149.                                     Response.End();  
  150.                                 }  
  151.                                 else  
  152.                                     throw new Exception("Bestandslocatie niet gevonden op server.");  
  153.                             }  
  154.                         }  
  155.                         if (_files.Count != 0)  
  156.                         {  
  157.                             UploadFiles(connection, transaction);  
  158.   
  159.                             var projatt2 = new ProjectAttachment();  
  160.                             AttachmentsGridView1.DataSource = projatt2.getProjectAttachmentsByProjectId(connection, transaction, ViewState["projectID"]);  
  161.                             AttachmentsGridView1.DataBind();  
  162.                         }  
  163.   
  164.                         if (Proj.Status != ProjectStatusEnum.CONCEPT && Proj.Status != ProjectStatusEnum.OFFERTE)  
  165.                         {  
  166.                             Looptijd.Enabled = false;  
  167.                             ResellerLevertHardware.Enabled = false;  
  168.                             chkInstallationPartner.Enabled = false;  
  169.                             HostedTelefonieStatusDropDownList.Enabled = false;  
  170.                             ProductTypeDropDownList.Enabled = false;  
  171.                             PartitionNameDropDownList.Enabled = false;  
  172.                             chkAllowPartnerAccess.Enabled = false;  
  173.                         }  
  174.   
  175.                         if (ViewState["projectID"] != null && ViewState["locationID"] != null)  
  176.                         {  
  177.                             ProjectLeftMenu1.ProjectID = ViewState["projectID"].ToString();  
  178.                             ProjectLeftMenu1.ProjectLocationID = ViewState["locationID"].ToString();  
  179.                             contactPersoonPartner.Visible = false;  
  180.                             contactPersoonCustomer.Visible = false;  
  181.                         }  
  182.   
  183.                         if (!IsPostBack)  
  184.                         {  
  185.                             SetUpHostedTypeControls(ProjectHosted, true);  
  186.                             FillHostedTypeControls(connection, transaction, ProjectHosted);  
  187.                         }  
  188.   
  189.                         if (ProjectSipLogic.Validate(_log, connection, transaction, int.Parse(Proj.ID)) != 0)  
  190.                         {  
  191.                             WarningBox.Visible = true;  
  192.                             SIPOnbeperktMinimalDuration.Text = Resources.SipUnlimitedMinimalDuration;  
  193.                         }  
  194.   
  195.                         //BI6545: BT CPS Verwijderen uit Orion. CPS Selectieveld verwijderd. Indien CPS in database op true staat, moet de rij met CPS getoond worden.  
  196.                         trReadIsCPSProject.Visible = Proj.isCPSProject;  
  197.                     }  
  198.   
  199.                     transaction.Commmit(); // here i get the error it is represented in the exeption  
  200.                 }  
  201.             }  
  202.             catch (Exception ex)  
  203.             {  
  204.                 Log.WriteException(ex);  
  205.             }  
  206.         }  
 
  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  
  40.         ProjectAttachmentLogic.saveBinaryDataAttachment(connection, transaction, tempSaveDir, projAttID);  
  41.     }  
    1. public class ProjectAttachmentLogic  
    2. {  
    3.   
    4.     public static void saveBinaryDataAttachment(SqlConnection connection, SqlTransaction transaction, string ProjattFilename, string ProjattId)  
    5.     {  
    6.         var id = int.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.         }  
    16.     }  
    17. }  
      1. public class ProjectAttachmentDal  
      2. {  
      3.     public static void InsertBinaryDataAttachment(SqlConnection connection, SqlTransaction transaction, int id, byte[] file)  
      4.     {  
      5.         using (var sqlCommand = new SqlCommand("INSERT INTO proj.BinaryDataAttachments (ID, Data) Values(@Id, @Data)", connection))  
      6.         {  
      7.             sqlCommand.Transaction = transaction;  
      8.             sqlCommand.Parameters.Add("@ID", SqlDbType.Int, id).Value = id;  
      9.             sqlCommand.Parameters.Add("@Data", SqlDbType.VarBinary, file.Length).Value = file;  
      10.             sqlCommand.ExecuteNonQuery();  
      11.         }  
      12.     }  
      13. }  

Answers (2)