protected void SaveContract(bool PreContract)
        {
            if (Session["MachineCode"] == null)
            {
                Response.Redirect(Session["CallerPage"].ToString());
                return;
            }
            string mcode = Session["MachineCode"].ToString();

            if (mcode.Length != 8)
            {
                Response.Redirect(Session["CallerPage"].ToString());
                return;
            }

            if (CheckCalculations() == false)
            {
                SysErr.Text = "CheckCalculations";
                return;
            }
            Label_ERROR1.Text = Label_ERROR2.Text = Label_ERROR3.Text = Label_ERROR4.Text = "";
            if (txtWorkerNum.Text == "")
            {
                txtWorkerNum.Text = "0";
            }
            if (txtOurBazNum.Text == "")
            {
                txtOurBazNum.Text = "0";
            }
            if (txtOtherBazNum.Text == "")
            {
                txtOtherBazNum.Text = "0";
            }
            if (txtTimeOffNum.Text == "")
            {
                txtTimeOffTime.Text = txtTimeOffNum.Text = "0";
            }

            string xsal     = "1398";
            string xworkers = "";
            string xourbazs = "";
            string xothrbaz = "";
            string xtimeoff = "";


            if (FileUpload1.PostedFile != null && FileUpload1.FileName != "")
            {
                FileUpload1.PostedFile.SaveAs((xworkers = Path.GetTempFileName()));
            }
            if (FileUpload2.PostedFile != null && FileUpload2.FileName != "")
            {
                FileUpload2.PostedFile.SaveAs((xourbazs = Path.GetTempFileName()));
            }
            if (FileUpload3.PostedFile != null && FileUpload3.FileName != "")
            {
                FileUpload3.PostedFile.SaveAs((xothrbaz = Path.GetTempFileName()));
            }
            if (FileUpload4.PostedFile != null && FileUpload4.FileName != "")
            {
                FileUpload4.PostedFile.SaveAs((xtimeoff = Path.GetTempFileName()));
            }


            SqlCommand cmd = new SqlCommand(
                "TestNationalCodes",
                new SqlConnection(ConfigurationManager.ConnectionStrings["NewCnnStr"].ConnectionString)
                );

            cmd.CommandType    = CommandType.StoredProcedure;
            cmd.CommandTimeout = 900;

            SqlParameter param = new SqlParameter("@Sal", xsal);

            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);


            param           = new SqlParameter("@MachineCode", mcode);
            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);

            param           = new SqlParameter("@FileNameEmp", xworkers);
            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@FileNameBazOwn", xourbazs);
            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@FileNameBazOther", xothrbaz);
            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@FileNameEmpMorkh", xtimeoff);
            param.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(param);

            param           = new SqlParameter("@NumEmp", 0);
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@NumBazOwn", 0);
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@NumBazOther", 0);
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(param);
            param           = new SqlParameter("@NumEmpMorkh", 0);
            param.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(param);
            DataSet        ds  = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            try
            {
                sda.Fill(ds);
                GridView_Exist.DataSource = ds;
                GridView_Exist.DataMember = ds.Tables[0].TableName;
                GridView_Exist.DataBind();
            }
            catch (Exception ex)
            {
                SysErr.Text = ex.Message;
                Label1.Text = "خطا در فایل کدهای ملی";
                return;
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                return;
            }

            if (getValue(cmd.Parameters["@NumEmp"].Value) != txtWorkerNum.Text)
            {
                Label_ERROR1.Text = ERR_CONT_NOEQ + getValue(cmd.Parameters["@NumEmp"].Value);
            }
            if (getValue(cmd.Parameters["@NumBazOwn"].Value) != txtOurBazNum.Text)
            {
                Label_ERROR2.Text = ERR_CONT_NOEQ + getValue(cmd.Parameters["@NumBazOwn"].Value);
            }

            if (getValue(cmd.Parameters["@NumBazOther"].Value) != txtOtherBazNum.Text)
            {
                Label_ERROR3.Text = ERR_CONT_NOEQ + getValue(cmd.Parameters["@NumBazOther"].Value);
            }
            if (getValue(cmd.Parameters["@NumEmpMorkh"].Value) != txtTimeOffNum.Text)
            {
                Label_ERROR4.Text = ERR_CONT_NOEQ + getValue(cmd.Parameters["@NumEmpMorkh"].Value);
            }

            if (Label_ERROR1.Text.Length > 0 || Label_ERROR2.Text.Length > 0 || Label_ERROR3.Text.Length > 0 || Label_ERROR4.Text.Length > 0)
            {
                return;
            }

            SysErr.Text = "main";


            if (xworkers.Length > 0)
            {
                File.Delete(xworkers);
            }
            if (xourbazs.Length > 0)
            {
                File.Delete(xourbazs);
            }
            if (xothrbaz.Length > 0)
            {
                File.Delete(xothrbaz);
            }
            if (xtimeoff.Length > 0)
            {
                File.Delete(xtimeoff);
            }
            if (txtTimeOffNum.Text.Length == 0)
            {
                txtTimeOffNum.Text = "0";
            }
            if (txtTimeOffTime.Text.Length == 0)
            {
                txtTimeOffTime.Text = "0";
            }


            bool    SavedSuccess = false;
            Int64   IntFishSum   = 0;
            DataSet depositDs    = new DataSet();

            string[] fishes;

            MsgBox.Text = "";



            if (HiddenFishField.Text.Length > 0) // now decode fishrows
            {
                fishes = HiddenFishField.Text.Split(new char[] { RECORD_SEPERATOR });
                DataSet FishesDataSet = new DataSet();
                for (int i = 0; i < (fishes.Length); ++i)
                {
                    string[] dataf = fishes[i].Split(new char[] { FILD_SEPERATOR });
                    FishesDataSet.Tables[0].Rows.Add(dataf);
                }

                //  DepositData data = new DepositData();
                depositDs = DataAccessFactory.CreateDepositData().CheckDepositValues(FishesDataSet.Tables[0]);

                if (depositDs != null && depositDs.Tables.Count > 0 && depositDs.Tables[0].Rows.Count > 0)
                {
                    var result = depositDs.Tables[0].AsEnumerable()
                                 .Where(myRow => myRow.Field <string>("depositStatus") == "bad");
                    if (result.ToList().Count > 0)
                    {
                        HiddenFishField.Text = "";
                        SysErr.Text          = "Err in fish";
                        MsgBox.Text          = "Fish_Not_Found";
                        return;
                    }
                }
            }
            if (CheckCalculations() == false)
            {
                SavedSuccess = false;
                SysErr.Text  = "CheckCalculations";
                return;
            }

            int status = PreContract ? 0 : 1;

            PersianCalendar p        = new PersianCalendar();
            string          cdate    = p.GetYear(DateTime.Now).ToString("0000") + "/" + p.GetMonth(DateTime.Now).ToString("00") + "/" + p.GetDayOfMonth(DateTime.Now).ToString("00");
            string          unitcode = mcode;
            string          fish_sum = IntFishSum.ToString();

            if (fish_sum.Length == 0)
            {
                fish_sum = "0";
            }
            Int64 contract_prize = Convert.ToInt64(txtMustPay.Text);

            // prevent unproper fish stat & prevent to insert additional fishes
            if (contract_prize > IntFishSum || (contract_prize == 0 && IntFishSum > 0))
            {
                SavedSuccess = false;
                SysErr.Text  = "error:overprize";
                return;
            }

            int    StartTime = Convert.ToInt32(Session["ContractYear"].ToString() + "01");
            string wo_old    = txtWorkerNum_oldprice.Text; if (wo_old.Length == 0)
            {
                wo_old = "0";
            }
            string ou_old = txtOurBazNum_oldprice.Text; if (ou_old.Length == 0)
            {
                ou_old = "0";
            }
            string ot_old = txtOtherBazNum_oldprice.Text; if (ot_old.Length == 0)
            {
                ot_old = "0";
            }
            int xtype = Convert.ToInt32(Insure_Type.SelectedValue);

            ContractModel model = new ContractModel
            {
                ContractType      = xtype,
                Status            = status,
                ContractDate      = cdate,
                Organinsuredcode  = (int)Session["MachineCode"],
                Worker_num        = Convert.ToInt32(txtWorkerNum.Text),
                Worker_start      = StartTime,
                Worker_time       = Convert.ToInt32(txtWorkerTime.Text),
                Worker_pay        = Convert.ToInt64(txtWorkerSumPay.Text),
                Ourbaz_num        = Convert.ToInt32(txtOurBazNum.Text),
                Ourbaz_start      = StartTime,
                Ourbaz_time       = Convert.ToInt32(txtOurBazTime.Text),
                Ourbaz_pay        = Convert.ToInt64(txtOurBazSumPay.Text),
                Otherbaz_num      = Convert.ToInt32(txtOtherBazNum.Text),
                Otherbaz_start    = StartTime,
                Otherbaz_time     = Convert.ToInt32(txtOtherBazTime.Text),
                Otherbaz_pay      = Convert.ToInt64(txtOtherBazSumPay.Text),
                Timeoff_num       = Convert.ToInt32(txtTimeOffNum.Text),
                Timeoff_start     = StartTime,
                Timeoff_time      = Convert.ToInt32(txtTimeOffTime.Text),
                Timeoff_pay       = Convert.ToInt64(txtTimeOffPay.Text),
                Contract_prize    = Convert.ToInt64(txtMustPay.Text),
                Deposit_sum       = Convert.ToInt64(fish_sum),
                ContractYearMonth = StartTime,
                Wo_old            = Convert.ToInt32(wo_old),
                Ou_old            = Convert.ToInt32(ou_old),
                Ot_old            = Convert.ToInt32(ot_old),
                DepositItems      = depositDs.Tables[0]
            };

            try
            {
                // ContractData cData = new ContractData();
                string FormIdds = DataAccessFactory.CreateContractData().SubmitContract(model);
                SavedSuccess = true;
                Session["ContractFormId"] = FormIdds;
            }
            catch (Exception ex)
            {
                SysErr.Text  = "SYSERR:" + ex.ToString();
                SavedSuccess = false;
            }

            if (SavedSuccess == false)
            {
                if (MsgBox.Text.Length == 0)
                {
                    MsgBox.Text = "Err_In_Save_Try_Again";
                }
            }
            if (PreContract == false)
            {
                Response.Redirect("~/Insurance/ContractPrint97.aspx");
            }
        }