protected void grdInsurances_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("insurancesEdit");

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                int insuranceId = (int)e.Keys["InsuranceCompanyID"];
                int refId = (int)e.Keys["RefID"];

                int companyId = Int32.Parse(hdfCompanyId.Value);
                decimal rate = Decimal.Parse(((TextBox)grdInsurances.Rows[e.RowIndex].Cells[0].FindControl("tbxRateEdit")).Text.Trim());
                DateTime startDate = ((RadDatePicker)grdInsurances.Rows[e.RowIndex].Cells[0].FindControl("tkrdpStartDateEdit")).SelectedDate.Value;
                DateTime endDate = ((RadDatePicker)grdInsurances.Rows[e.RowIndex].Cells[0].FindControl("tkrdpEndDateEdit")).SelectedDate.Value;

                // Update data
                ProjectCostingSheetInformationInsurancesInformation model = new ProjectCostingSheetInformationInsurancesInformation(projectCostingSheetInformationTDS);
                model.Update(costingSheetId, insuranceId, refId, rate, false, companyId, startDate, endDate);

                // Store dataset
                insurancesInformation = (ProjectCostingSheetInformationTDS.InsurancesInformationDataTable)model.Table;
                Session["insurancesInformation"] = insurancesInformation;
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                StepInsurancesInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        ///////////////////////////////
        protected void grdInsurances_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Insurances Gridview, if the gridview is edition mode
                    if (grdInsurances.EditIndex >= 0)
                    {
                        grdInsurances.UpdateRow(grdInsurances.EditIndex, true);
                    }

                    // Validate general data
                    Page.Validate("insurancesNew");

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        int insuranceId = Int32.Parse(((DropDownList)grdInsurances.FooterRow.FindControl("ddlInsuranceNew")).SelectedValue);
                        string insurance = ((DropDownList)grdInsurances.FooterRow.FindControl("ddlInsuranceNew")).SelectedItem.Text;
                        decimal rate = Decimal.Parse(((TextBox)grdInsurances.FooterRow.FindControl("tbxRateNew")).Text.Trim());

                        DateTime startDate = ((RadDatePicker)grdInsurances.FooterRow.FindControl("tkrdpStartDateNew")).SelectedDate.Value;
                        DateTime endDate = ((RadDatePicker)grdInsurances.FooterRow.FindControl("tkrdpEndDateNew")).SelectedDate.Value;

                        ProjectCostingSheetInformationInsurancesInformation model = new ProjectCostingSheetInformationInsurancesInformation(projectCostingSheetInformationTDS);
                        model.Insert(0, insuranceId, rate, false, companyId, insurance, startDate, endDate);

                        Session.Remove("insurancesInformationDummy");
                        insurancesInformation = (ProjectCostingSheetInformationTDS.InsurancesInformationDataTable)model.Table;
                        Session["insurancesInformation"] = insurancesInformation;
                        Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                        grdInsurances.DataBind();

                        StepInsurancesInformationProcessGrid();
                    }
                    break;
            }
        }
        protected void grdInsurances_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Insurances Gridview, if the gridview is edition mode
            if (grdInsurances.EditIndex >= 0)
            {
                grdInsurances.UpdateRow(grdInsurances.EditIndex, true);
            }

            // Delete insurance
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            int insuranceId = (int)e.Keys["InsuranceCompanyID"];
            int refId = (int)e.Keys["RefID"];

            ProjectCostingSheetInformationInsurancesInformation model = new ProjectCostingSheetInformationInsurancesInformation(projectCostingSheetInformationTDS);
            model.Delete(costingSheetId, insuranceId, refId);

            // Store dataset
            insurancesInformation = (ProjectCostingSheetInformationTDS.InsurancesInformationDataTable)model.Table;
            Session["insurancesInformation"] = insurancesInformation;
            Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

            StepInsurancesInformationProcessGrid();
        }
        private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int costingSheetId = Int32.Parse(hdfCostingSheetId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                ProjectCostingSheetInformationBasicInformation projectCostingSheetInformationBasicInformation = new ProjectCostingSheetInformationBasicInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBasicInformation.Save(companyId);

                // Save costs information
                ProjectCostingSheetInformationLabourHoursInformation projectCostingSheetInformationLabourHoursInformation = new ProjectCostingSheetInformationLabourHoursInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationLabourHoursInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationUnitsInformation projectCostingSheetInformationUnitsInformation = new ProjectCostingSheetInformationUnitsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationUnitsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationMaterialsInformation projectCostingSheetInformationMaterialsInformation = new ProjectCostingSheetInformationMaterialsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationMaterialsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationOtherCostsInformation projectCostingSheetInformationOtherCostsInformation = new ProjectCostingSheetInformationOtherCostsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationOtherCostsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationSubcontractorsInformation projectCostingSheetInformationSubcontractorsInformation = new ProjectCostingSheetInformationSubcontractorsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationSubcontractorsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationHotelsInformation projectCostingSheetInformationHotelsInformation = new ProjectCostingSheetInformationHotelsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationHotelsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationBondingsInformation projectCostingSheetInformationBondingsInformation = new ProjectCostingSheetInformationBondingsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBondingsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationInsurancesInformation projectCostingSheetInformationInsurancesInformation = new ProjectCostingSheetInformationInsurancesInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationInsurancesInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationOtherCategoryInformation projectCostingSheetInformationOtherCategoryInformation = new ProjectCostingSheetInformationOtherCategoryInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationOtherCategoryInformation.Save(companyId, costingSheetId);

                DB.CommitTransaction();

                // Store datasets
                projectCostingSheetInformationTDS.AcceptChanges();
                Session["lfsProjectTDS"] = projectTDS;
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;
                Session["labourHoursInformation"] = projectCostingSheetInformationTDS.LabourHoursInformation;
                Session["unitsInformation"] = projectCostingSheetInformationTDS.UnitsInformation;
                Session["subcontractorsInformation"] = projectCostingSheetInformationTDS.SubcontractorsInformation;
                Session["materialsInformation"] = projectCostingSheetInformationTDS.MaterialsInformation;
                Session["otherCostsInformation"] = projectCostingSheetInformationTDS.OtherCostsInformation;

                Session["hotelssInformation"] = projectCostingSheetInformationTDS.HotelsInformation;
                Session["bondingsInformation"] = projectCostingSheetInformationTDS.BondingsInformation;
                Session["insurancesInformation"] = projectCostingSheetInformationTDS.InsurancesInformation;
                Session["otherCategoryInformation"] = projectCostingSheetInformationTDS.OtherCategoryInformation;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }