private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int unitId = Int32.Parse(hdfUnitId.Value);

            LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway(libraryTDSForUnits);
            libraryFilesGateway.Update();

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // ... if can edit everything
                if (Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_EDIT"]))
                {
                    // Save inspection details
                    UnitInformationInspectionDetails unitInformationInspectionDetails = new UnitInformationInspectionDetails(unitInformationTDS);
                    unitInformationInspectionDetails.Save(unitId, companyId);

                    // Save notes information
                    UnitInformationNoteDetailsGateway unitInformationNoteDetailsGateway = new UnitInformationNoteDetailsGateway(unitInformationTDS);
                    UnitInformationNoteDetails unitInformationNoteDetails = new UnitInformationNoteDetails(unitInformationTDS);

                    foreach (UnitInformationTDS.NoteInformationRow rowNotes in (UnitInformationTDS.NoteInformationDataTable)unitInformationNoteDetailsGateway.Table)
                    {
                        if (!rowNotes.IsLIBRARY_FILES_IDNull())
                        {
                            if (rowNotes.LIBRARY_FILES_ID == 0 && rowNotes.FILENAME != "")
                            {
                                libraryFilesGateway.LoadByFileName(rowNotes.FILENAME, companyId);
                                int newLibraryFilesId = libraryFilesGateway.GetlibraryFilesId(rowNotes.FILENAME);

                                rowNotes.LIBRARY_FILES_ID = newLibraryFilesId;
                            }
                        }
                    }

                    unitInformationNoteDetails.Save(companyId);

                    // Save costs information
                    UnitInformationCostInformation unitInformationCostInformation = new UnitInformationCostInformation(unitInformationTDS);
                    unitInformationCostInformation.Save(companyId);

                    // Save costs exceptions information
                    UnitInformationCostExceptionsInformation unitInformationCostExceptionsInformation = new UnitInformationCostExceptionsInformation(unitInformationTDS);
                    unitInformationCostExceptionsInformation.Save(companyId, unitId);

                    // Save unit details
                    UnitInformationUnitDetails unitInformationUnitDetails = new UnitInformationUnitDetails(unitInformationTDS);
                    unitInformationUnitDetails.Save(arrayCategoriesSelectedForEdit, companyId);
                }

                // ... if could only edit notes
                if (!Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_EDIT"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_COMMENTS"]))
                {
                    // Save notes information
                    UnitInformationNoteDetailsGateway unitInformationNoteDetailsGateway = new UnitInformationNoteDetailsGateway(unitInformationTDS);
                    UnitInformationNoteDetails unitInformationNoteDetails = new UnitInformationNoteDetails(unitInformationTDS);

                    foreach (UnitInformationTDS.NoteInformationRow rowNotes in (UnitInformationTDS.NoteInformationDataTable)unitInformationNoteDetailsGateway.Table)
                    {
                        if (!rowNotes.IsLIBRARY_FILES_IDNull())
                        {
                            if (rowNotes.LIBRARY_FILES_ID == 0 && rowNotes.FILENAME != "")
                            {
                                libraryFilesGateway.LoadByFileName(rowNotes.FILENAME, companyId);
                                int newLibraryFilesId = libraryFilesGateway.GetlibraryFilesId(rowNotes.FILENAME);

                                rowNotes.LIBRARY_FILES_ID = newLibraryFilesId;
                            }
                        }
                    }

                    unitInformationNoteDetails.Save(companyId);
                }

                DB.CommitTransaction();

                // Store datasets
                unitInformationTDS.AcceptChanges();
                libraryTDSForUnits.AcceptChanges();
                Session["unitInformationTDS"] = unitInformationTDS;
                Session["libraryTDSForUnits"] = libraryTDSForUnits;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void GrdNotesAdd()
        {
            if (ValidateNotesFooter())
            {
                Page.Validate("notesDataAdd");
                if (Page.IsValid)
                {
                    int unitId = Int32.Parse(hdfUnitId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    string newSubject = ((TextBox)grdNotes.FooterRow.FindControl("tbxNoteSubjectNew")).Text.Trim();
                    int loginId = Convert.ToInt32(Session["loginID"]);
                    DateTime dateTime_ = DateTime.Now;
                    string newNote = ((TextBox)grdNotes.FooterRow.FindControl("tbxNoteNoteNew")).Text.Trim();
                    bool inDatabase = false;
                    int? libraryFilesId = null;
                    string fileName = ((Label)grdNotes.FooterRow.FindControl("lblFileNameNoteAttachmentNew")).Text.Trim();
                    if (fileName != "")
                    {
                        LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway();
                        libraryFilesId = libraryFilesGateway.GetlibraryFilesId(fileName);
                    }

                    UnitInformationNoteDetails model = new UnitInformationNoteDetails(unitInformationTDS);
                    model.Insert(unitId, newSubject, loginId, dateTime_, newNote, false, companyId, inDatabase, libraryFilesId);

                    Session.Remove("unitsNotesTempDummy");
                    Session["unitInformationTDS"] = unitInformationTDS;

                    grdNotes.DataBind();
                    grdNotes.PageIndex = grdNotes.PageCount - 1;
                }
            }
        }
        private void GrdNotesDeleteAttachment(int libraryFileId, int refId)
        {
            // Button delete functionality
            if (libraryFileId != 0)
            {
                UnitInformationNoteDetails model = new UnitInformationNoteDetails(unitInformationTDS);
                model.UpdateLibraryFilesId(int.Parse(hdfUnitId.Value), refId, null, "", "");

                LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway(libraryTDSForUnits);
                libraryFilesGateway.DeleteByLibraryFilesId(libraryFileId);

                ViewState["update"] = "no";

                Session["unitInformationTDS"] = unitInformationTDS;
                Session["libraryTDSForUnits"] = libraryTDSForUnits;

                grdNotes.DataBind();
            }
        }
        protected void grdNotes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Notes Gridview, if the gridview is edition mode
            if (grdNotes.EditIndex >= 0)
            {
                grdNotes.UpdateRow(grdNotes.EditIndex, true);
            }

            // Delete notes
            int unitId = (int)e.Keys["UnitID"];
            int refId = (int)e.Keys["RefID"];

            UnitInformationNoteDetails model = new UnitInformationNoteDetails(unitInformationTDS);
            model.Delete(unitId, refId);

            if (((Label)grdNotes.Rows[e.RowIndex].Cells[3].FindControl("lblLibraryFileId")).Text.Trim() != "")
            {
                int? libraryFilesId = Int32.Parse(((Label)grdNotes.Rows[e.RowIndex].Cells[3].FindControl("lblLibraryFileId")).Text.Trim());

                LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway();
                libraryFilesGateway.DeleteByLibraryFilesId((int)libraryFilesId);
            }

            // Store dataset
            Session["unitInformationTDS"] = unitInformationTDS;
        }
        /// <summary>
        /// UpdateForProcess. Update the author of each Note
        /// </summary>
        public void UpdateForProcess()
        {
            LoginGateway loginGateway = new LoginGateway();
            LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway();

            foreach (ProjectTDS.LFS_PROJECT_NOTERow row in (ProjectTDS.LFS_PROJECT_NOTEDataTable)Table)
            {
                loginGateway.LoadByLoginId(row.LoginID, row.COMPANY_ID);
                row.UserFullName = loginGateway.GetLastName(row.LoginID, row.COMPANY_ID) + " " + loginGateway.GetFirstName(row.LoginID, row.COMPANY_ID);
                row.CreationDateTime = row.DateTime.ToString();
                if (!row.IsLIBRARY_FILES_IDNull())
                {
                    try
                    {
                        libraryFilesGateway.LoadByLibraryFilesId(row.LIBRARY_FILES_ID);
                        row.ORIGINAL_FILENAME = libraryFilesGateway.GetOriginalFileName(row.LIBRARY_FILES_ID);
                        row.FILENAME = libraryFilesGateway.GetFileName(row.LIBRARY_FILES_ID);
                    }
                    catch { }
                }
            }
        }
        private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);

            LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway(libraryTDSForServices);
            libraryFilesGateway.Update();

            DB.Open();
            DB.BeginTransaction();
            try
            {
                ServiceInformationServiceNoteGateway serviceInformationServiceNoteGateway = new ServiceInformationServiceNoteGateway(serviceInformationTDS);
                ServiceInformationServiceNote serviceInformationServiceNote = new ServiceInformationServiceNote(serviceInformationTDS);

                foreach (ServiceInformationTDS.NoteInformationRow rowNotes in (ServiceInformationTDS.NoteInformationDataTable)serviceInformationServiceNoteGateway.Table)
                {
                    if (!rowNotes.IsLIBRARY_FILES_IDNull())
                    {
                        if (rowNotes.LIBRARY_FILES_ID == 0 && rowNotes.FILENAME != "")
                        {
                            libraryFilesGateway.LoadByFileName(rowNotes.FILENAME, companyId);
                            int newLibraryFilesId = libraryFilesGateway.GetlibraryFilesId(rowNotes.FILENAME);

                            rowNotes.LIBRARY_FILES_ID = newLibraryFilesId;
                        }
                    }
                }

                // Save costs information
                ServiceInformationServiceCost serviceInformationServiceCost = new ServiceInformationServiceCost(serviceInformationTDS);
                serviceInformationServiceCost.Save(companyId);

                // Save notes information
                serviceInformationServiceNote.Save(companyId);

                // Save service information
                ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation(serviceInformationTDS);
                serviceInformationBasicInformation.Save(companyId);

                DB.CommitTransaction();

                // Store datasets
                libraryTDSForServices.AcceptChanges();
                serviceInformationTDS.AcceptChanges();
                Session["serviceInformationTDS"] = serviceInformationTDS;
                Session["libraryTDSForServices"] = libraryTDSForServices;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void GrdNotesDeleteAttachment(int libraryFileId, int refId)
        {
            // Button delete functionality
            if (libraryFileId != 0)
            {
                ServiceInformationServiceNote model = new ServiceInformationServiceNote(serviceInformationTDS);
                model.UpdateLibraryFilesId(int.Parse(hdfServiceId.Value), refId, null, "", "");

                LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway(libraryTDSForServices);
                libraryFilesGateway.DeleteByLibraryFilesId(libraryFileId);

                ViewState["update"] = "no";

                Session["serviceInformationTDS"] = serviceInformationTDS;
                Session["libraryTDSForServices"] = libraryTDSForServices;
                grdNotes.DataBind();
                grdCosts.DataBind();
            }
        }
        private void ProcessUpload2(int projectId, int refId, int companyId)
        {
            LibraryFilesGateway libraryFilesGateway = new LibraryFilesGateway();

            try
            {
                //... Set the default path to store uploaded files.
                AppSettingsReader appSettingReader = new AppSettingsReader();
                string websitePath = appSettingReader.GetValue("WebsitePath", typeof(System.String)).ToString();

                string filename = "";

                //... Get the extension of file
                string[] shortFilename = nuifAttachment.FileName.Split(new char[] { '.' });
                string ext = "." + shortFilename[shortFilename.Length - 1];

                bool exit = false;

                while (!exit)
                {
                    StringBuilder builder = new StringBuilder();
                    Random rand = new Random();
                    char ch;

                    for (int i = 0; i < 8; i++)
                    {
                        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rand.NextDouble() + 65)));
                        builder.Append(ch);
                    }

                    filename = builder.ToString();
                    filename = filename + ext;

                    libraryFilesGateway.LoadByFileName(filename, companyId);

                    if (libraryFilesGateway.Table.Rows.Count > 0)
                    {
                        exit = false;
                    }
                    else
                    {
                        exit = true;
                    }
                }

                string pathFull = websitePath + "\\Files\\LF_DGHOUGDH\\Library\\";

                //... Save the file
                nuifAttachment.MoveTo(Path.Combine(pathFull, filename), MoveToOptions.Overwrite);

                LibraryFiles libraryFiles = new LibraryFiles(libraryTDS);
                libraryFiles.Insert(filename, tbxDescription.Text, nuifAttachment.FileName, null, Convert.ToInt32(ViewState["libraryCategoriesId"]), Convert.ToInt32(hdfLoginId.Value), int.Parse(Session["companyID"].ToString()), nuifAttachment.FileContent.Length.ToString()); //Note: COMPANY_ID

                ProjectNavigatorProjectNotes model = new ProjectNavigatorProjectNotes(projectNavigatorTDS);
                model.UpdateLibraryFilesId(projectId, refId, 0, nuifAttachment.FileName, filename);

                Session["lfsProjectTDS"] = projectTDS;
                Session["lfsLibraryTDS"] = libraryTDS;
                Session["projectNavigatorTDS"] = projectNavigatorTDS;
                Session["fromAttachment"] = "yes";
            }
            catch (Exception ex)
            {
            }
        }