private void BindGridDetail()
        {
            var lstBudgetDetail = new List <view_Budget_transfer_detail>();

            try
            {
                cBudget_transfer oBudget_transfer = new cBudget_transfer();
                _strMessage          = string.Empty;
                _strCriteria         = " and budget_transfer_doc = '" + txtbudget_transfer_doc.Text + "' order by lot_code_from";
                lstBudgetDetail      = oBudget_transfer.GETDETAILS(_strCriteria);
                GridView1.DataSource = lstBudgetDetail;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
            finally
            {
                this.bIsGridDetailEmpty = false;
                if (!lstBudgetDetail.Any())
                {
                    this.bIsGridDetailEmpty = true;
                    EmptyGridFix(GridView1);
                }
                else
                {
                    GridView1.DataBind();
                }
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string           strMessage = string.Empty;
            string           strScript  = string.Empty;
            HiddenField      hddbudget_transfer_detail_id = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hddbudget_transfer_detail_id");
            cBudget_transfer oBudget_transfer             = new cBudget_transfer();

            try
            {
                oBudget_transfer.SP_BUDGET_TRANSFER_DETAIL_DEL(hddbudget_transfer_detail_id.Value);
                oBudget_transfer.SP_BUDGET_TRANSFER_TOTAL_UPD(txtbudget_transfer_doc.Text);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("REFERENCE constraint"))
                {
                    MsgBox("ไม่สามารถลบข้อมูลได้เนื่องจากมีการนำไปใช้ในระบบแล้ว");
                }
                else
                {
                    lblError.Text = ex.Message.ToString();
                }
            }
            finally
            {
                oBudget_transfer.Dispose();
            }
            BindGridDetail();
        }
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
     string strMessage = string.Empty;
     string strCheck = string.Empty;
     string strScript = string.Empty;
     string strUpdatedBy = Session["username"].ToString();
     Label lblbudget_transfer_doc = (Label)GridView1.Rows[e.RowIndex].FindControl("lblbudget_transfer_doc");
     cBudget_transfer oBudget_transfer = new cBudget_transfer();
     try
     {
         oBudget_transfer.SP_BUDGET_TRANSFER_HEAD_DEL(lblbudget_transfer_doc.Text);
     }
     catch (Exception ex)
     {
         if (ex.Message.Contains("REFERENCE constraint"))
         {
             MsgBox("ไม่สามารถลบข้อมูลได้เนื่องจากมีการนำไปใช้ในระบบแล้ว");
         }
         else
         {
             lblError.Text = ex.Message.ToString();
         }
     }
     finally
     {
         oBudget_transfer.Dispose();
     }
     BindGridView(0);
 }
        private bool saveData()
        {
            bool blnResult = false;
            Budget_transfer_head budget_transfer_head = new Budget_transfer_head();
            cBudget_transfer     oBudget_transfer     = new cBudget_transfer();

            try
            {
                #region set Data
                budget_transfer_head.budget_transfer_doc    = txtbudget_transfer_doc.Text;
                budget_transfer_head.budget_doc_no          = txtbudget_doc_no.Text;
                budget_transfer_head.budget_transfer_year   = cboYear.SelectedValue;
                budget_transfer_head.budget_transfer_date   = cCommon.GetDate(txtbudget_transfer_date.Text);
                budget_transfer_head.budget_type            = cboBudget_type.SelectedValue;
                budget_transfer_head.degree_code_from       = cboDegree_from.SelectedValue;
                budget_transfer_head.major_code_from        = cboMajor_from.SelectedValue;
                budget_transfer_head.budget_plan_code_from  = txtbudget_plan_code_from.Text;
                budget_transfer_head.degree_code_to         = cboDegree_to.SelectedValue;
                budget_transfer_head.major_code_to          = cboMajor_to.SelectedValue;
                budget_transfer_head.budget_plan_code_to    = txtbudget_plan_code_to.Text;
                budget_transfer_head.budget_transfer_remark = txtbudget_transfer_remark.Text;
                budget_transfer_head.c_created_by           = Session["username"].ToString();
                budget_transfer_head.c_updated_by           = Session["username"].ToString();
                #endregion
                if (ViewState["mode"].ToString().ToLower().Equals("edit"))
                {
                    oBudget_transfer.SP_BUDGET_TRANSFER_HEAD_UPD(budget_transfer_head);
                }
                else
                {
                    oBudget_transfer.SP_BUDGET_TRANSFER_HEAD_INS(budget_transfer_head);
                    ViewState["budget_transfer_doc"] = budget_transfer_head.budget_transfer_doc;
                }
                blnResult = true;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("duplicate key"))
                {
                    MsgBox("ข้อมูลซ้ำโปรดตรวจสอบ");
                }
                else
                {
                    lblError.Text = ex.Message.ToString();
                }
            }
            finally
            {
                oBudget_transfer.Dispose();
            }
            return(blnResult);
        }
        private bool saveData()
        {
            bool             blnResult        = false;
            string           strScript        = string.Empty;
            cBudget_transfer oBudget_transfer = new cBudget_transfer();
            DataSet          ds = new DataSet();

            try
            {
                #region set Data
                var budget_transfer_detail = new Budget_transfer_detail()
                {
                    budget_transfer_detail_id     = BudgetTransferDetailId,
                    budget_transfer_doc           = BudgetTransferDoc,
                    budget_money_major_id_from    = long.Parse(hddbudget_money_major_id_from.Value),
                    budget_money_major_id_to      = long.Parse(hddbudget_money_major_id_to.Value),
                    budget_transfer_detail_amount = decimal.Parse(txtbudget_transfer_detail_amount.Value.ToString()),
                    budget_transfer_detail_remark = txtbudget_transfer_detail_comment.Text.Trim(),
                    c_created_by = Session["username"].ToString(),
                    c_updated_by = Session["username"].ToString()
                };
                #endregion
                if (ViewState["mode"].ToString().ToLower().Equals("edit"))
                {
                    oBudget_transfer.SP_BUDGET_TRANSFER_DETAIL_UPD(budget_transfer_detail);
                }
                else
                {
                    oBudget_transfer.SP_BUDGET_TRANSFER_DETAIL_INS(budget_transfer_detail);
                }
                oBudget_transfer.SP_BUDGET_TRANSFER_TOTAL_UPD(BudgetTransferDoc);
                blnResult = true;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("duplicate key"))
                {
                    MsgBox("ข้อมูลซ้ำโปรดตรวจสอบ");
                }
                else
                {
                    lblError.Text = ex.Message.ToString();
                }
            }
            finally
            {
                oBudget_transfer.Dispose();
            }
            return(blnResult);
        }
        private void SetBudgetTransferData()
        {
            cBudget_transfer oBudget_transfer = new cBudget_transfer();
            string           strMessage = string.Empty, strCriteria = string.Empty;

            try
            {
                strCriteria = " and budget_transfer_doc = '" + this.BudgetTransferDoc + "' ";
                this.MyBudgetTransferHead = oBudget_transfer.GET(strCriteria);
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }
        private void setData()
        {
            view_Budget_transfer_head budget_transfer_head = null;
            cBudget_transfer          oBudget_transfer     = new cBudget_transfer();

            try
            {
                _strCriteria         = " and budget_transfer_doc = '" + ViewState["budget_transfer_doc"].ToString() + "' ";
                budget_transfer_head = oBudget_transfer.GET(_strCriteria);
                if (budget_transfer_head != null)
                {
                    #region set Control
                    TabContainer1.Tabs[1].Visible = true;
                    //Tab 1
                    txtUpdatedBy.Text            = budget_transfer_head.c_updated_by;
                    txtUpdatedDate.Text          = budget_transfer_head.d_updated_date.ToString();
                    txtbudget_transfer_doc.Text  = budget_transfer_head.budget_transfer_doc;
                    txtbudget_transfer_date.Text = budget_transfer_head.budget_transfer_date.Value.ToString("dd/MM/yyyy");
                    txtbudget_doc_no.Text        = budget_transfer_head.budget_doc_no;

                    InitcboBudgetType();
                    if (cboBudget_type.Items.FindByValue(budget_transfer_head.budget_type) != null)
                    {
                        cboBudget_type.SelectedIndex = -1;
                        cboBudget_type.Items.FindByValue(budget_transfer_head.budget_type).Selected = true;
                    }

                    InitcboYear();
                    if (cboYear.Items.FindByValue(budget_transfer_head.budget_transfer_year) != null)
                    {
                        cboYear.SelectedIndex = -1;
                        cboYear.Items.FindByValue(budget_transfer_head.budget_transfer_year).Selected = true;
                    }

                    InitcboDegree_from();
                    if (cboDegree_from.Items.FindByValue(budget_transfer_head.degree_code_from) != null)
                    {
                        cboDegree_from.SelectedIndex = -1;
                        cboDegree_from.Items.FindByValue(budget_transfer_head.degree_code_from).Selected = true;
                    }

                    InitcboMajor_from();
                    if (cboMajor_from.Items.FindByValue(budget_transfer_head.major_code_from) != null)
                    {
                        cboMajor_from.SelectedIndex = -1;
                        cboMajor_from.Items.FindByValue(budget_transfer_head.major_code_from).Selected = true;
                    }

                    txtbudget_plan_code_from.Text = budget_transfer_head.budget_plan_code_from;
                    txtbudget_name_from.Text      = budget_transfer_head.budget_name_from;
                    txtproduce_name_from.Text     = budget_transfer_head.produce_name_from;
                    txtactivity_name_from.Text    = budget_transfer_head.activity_name_from;
                    txtplan_name_from.Text        = budget_transfer_head.plan_name_from;
                    txtwork_name_from.Text        = budget_transfer_head.work_name_from;
                    txtfund_name_from.Text        = budget_transfer_head.fund_name_from;
                    txtunit_name_from.Text        = budget_transfer_head.unit_name_from;

                    InitcboDegree_to();
                    if (cboDegree_to.Items.FindByValue(budget_transfer_head.degree_code_to) != null)
                    {
                        cboDegree_to.SelectedIndex = -1;
                        cboDegree_to.Items.FindByValue(budget_transfer_head.degree_code_to).Selected = true;
                    }

                    InitcboMajor_to();
                    if (cboMajor_to.Items.FindByValue(budget_transfer_head.major_code_to) != null)
                    {
                        cboMajor_to.SelectedIndex = -1;
                        cboMajor_to.Items.FindByValue(budget_transfer_head.major_code_to).Selected = true;
                    }

                    txtbudget_plan_code_to.Text = budget_transfer_head.budget_plan_code_to;
                    txtbudget_name_to.Text      = budget_transfer_head.budget_name_to;
                    txtproduce_name_to.Text     = budget_transfer_head.produce_name_to;
                    txtactivity_name_to.Text    = budget_transfer_head.activity_name_to;
                    txtplan_name_to.Text        = budget_transfer_head.plan_name_to;
                    txtwork_name_to.Text        = budget_transfer_head.work_name_to;
                    txtfund_name_to.Text        = budget_transfer_head.fund_name_to;
                    txtunit_name_to.Text        = budget_transfer_head.unit_name_to;

                    txtbudget_transfer_remark.Text = budget_transfer_head.budget_transfer_remark;
                    BindGridDetail();

                    #endregion
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }
        private void setData()
        {
            cBudget_transfer oBudget_transfer = new cBudget_transfer();
            string           strMessage = string.Empty, strCriteria = string.Empty;

            try
            {
                strCriteria = " and budget_transfer_detail_id = '" + BudgetTransferDetailId + "' ";
                var item = oBudget_transfer.GETDETAIL(strCriteria);
                if (item != null)
                {
                    #region set Control

                    InitcboLot_from();
                    if (cboLot_from.Items.FindByValue(item.lot_code_from) != null)
                    {
                        cboLot_from.SelectedIndex = -1;
                        cboLot_from.Items.FindByValue(item.lot_code_from).Selected = true;
                    }
                    InitcboItem_group_from();
                    if (cboItem_group_from.Items.FindByValue(item.item_group_code_from) != null)
                    {
                        cboItem_group_from.SelectedIndex = -1;
                        cboItem_group_from.Items.FindByValue(item.item_group_code_from).Selected = true;
                    }

                    InitcboItemGroupDetail_from();
                    if (cboItem_group_detail_from.Items.FindByValue(item.item_group_detail_id_from.ToString()) != null)
                    {
                        cboItem_group_detail_from.SelectedIndex = -1;
                        cboItem_group_detail_from.Items.FindByValue(item.item_group_detail_id_from.ToString()).Selected = true;
                    }

                    InitcboItem_from();
                    if (cboItem_from.Items.FindByValue(item.item_code_from.ToString()) != null)
                    {
                        cboItem_from.SelectedIndex = -1;
                        cboItem_from.Items.FindByValue(item.item_code_from.ToString()).Selected = true;
                    }

                    InitcboItem_detail_from();
                    if (cboItem_detail_from.Items.FindByValue(item.item_detail_id_from.ToString()) != null)
                    {
                        cboItem_detail_from.SelectedIndex = -1;
                        cboItem_detail_from.Items.FindByValue(item.item_detail_id_from.ToString()).Selected = true;
                    }
                    hddbudget_money_major_id_from.Value = item.budget_money_major_id_from.ToString();

                    InitcboLot_to();
                    if (cboLot_to.Items.FindByValue(item.lot_code_to) != null)
                    {
                        cboLot_to.SelectedIndex = -1;
                        cboLot_to.Items.FindByValue(item.lot_code_to).Selected = true;
                    }
                    InitcboItem_group_to();
                    if (cboItem_group_to.Items.FindByValue(item.item_group_code_to) != null)
                    {
                        cboItem_group_to.SelectedIndex = -1;
                        cboItem_group_to.Items.FindByValue(item.item_group_code_to).Selected = true;
                    }

                    InitcboItemGroupDetail_to();
                    if (cboItem_group_detail_to.Items.FindByValue(item.item_group_detail_id_to.ToString()) != null)
                    {
                        cboItem_group_detail_to.SelectedIndex = -1;
                        cboItem_group_detail_to.Items.FindByValue(item.item_group_detail_id_to.ToString()).Selected = true;
                    }

                    InitcboItem_to();
                    if (cboItem_to.Items.FindByValue(item.item_code_to.ToString()) != null)
                    {
                        cboItem_to.SelectedIndex = -1;
                        cboItem_to.Items.FindByValue(item.item_code_to.ToString()).Selected = true;
                    }

                    InitcboItem_detail_to();
                    if (cboItem_detail_to.Items.FindByValue(item.item_detail_id_to.ToString()) != null)
                    {
                        cboItem_detail_to.SelectedIndex = -1;
                        cboItem_detail_to.Items.FindByValue(item.item_detail_id_to.ToString()).Selected = true;
                    }
                    hddbudget_money_major_id_to.Value = item.budget_money_major_id_to.ToString();

                    this.BudgetTransferDoc = item.budget_transfer_doc;
                    txtbudget_transfer_detail_comment.Text = item.budget_transfer_detail_remark;
                    txtbudget_transfer_detail_amount.Value = item.budget_transfer_detail_amount;
                    txtUpdatedBy.Text   = item.c_updated_by;
                    txtUpdatedDate.Text = item.d_updated_date.ToString();
                    #endregion
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }
        private void BindGridView(int nPageNo)
        {
            var oBudget_transfer = new cBudget_transfer();
            DataSet ds = new DataSet();
            string strMessage = string.Empty;
            string strCriteria = string.Empty;
            var budget_transfer_head = new view_Budget_transfer_head();
            string strScript = string.Empty;
            #region Criteria
            budget_transfer_head.budget_transfer_year = cboYear.SelectedValue;
            budget_transfer_head.budget_transfer_doc = txtbudget_plan_code_from.Text.Replace("'", "''").Trim();

            budget_transfer_head.degree_code_from = cboDegree_from.SelectedValue;
            budget_transfer_head.budget_plan_code_from = txtbudget_plan_code_from.Text;
            budget_transfer_head.unit_code_from = cboUnit_from.SelectedValue;
            budget_transfer_head.budget_code_from = cboBudget_from.SelectedValue;
            budget_transfer_head.produce_code_from = cboProduce_from.SelectedValue;
            budget_transfer_head.activity_code_from = cboActivity_from.SelectedValue;
            budget_transfer_head.major_code_from = cboMajor_from.SelectedValue;

            budget_transfer_head.degree_code_to = cboDegree_to.SelectedValue;
            budget_transfer_head.budget_plan_code_to = txtbudget_plan_code_to.Text;
            budget_transfer_head.unit_code_to = cboUnit_to.SelectedValue;
            budget_transfer_head.budget_code_to = cboBudget_to.SelectedValue;
            budget_transfer_head.produce_code_to = cboProduce_to.SelectedValue;
            budget_transfer_head.activity_code_to = cboActivity_to.SelectedValue;
            budget_transfer_head.major_code_to = cboMajor_to.SelectedValue;


            if (!string.IsNullOrEmpty(budget_transfer_head.budget_transfer_year))
            {
                strCriteria = strCriteria + "  And  (budget_transfer_year = '" + budget_transfer_head.budget_transfer_year + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.budget_transfer_doc))
            {
                strCriteria = strCriteria + "  And  (budget_transfer_doc LIKE '%" + budget_transfer_head.budget_transfer_doc + "%') ";
            }

            if (!string.IsNullOrEmpty(txtdate_begin.Text))
            {
                strCriteria = strCriteria + "  And  (budget_transfer_date >= '" + cCommon.SeekDate(txtdate_begin.Text) + "') ";
            }

            if (!string.IsNullOrEmpty(txtdate_end.Text))
            {
                strCriteria = strCriteria + "  And  (budget_transfer_date <= '" + cCommon.SeekDate(txtdate_end.Text) + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.degree_code_from))
            {
                strCriteria = strCriteria + "  And  (degree_code_from ='" + budget_transfer_head.degree_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.budget_plan_code_from))
            {
                strCriteria = strCriteria + "  And  (budget_plan_code_from ='" + budget_transfer_head.budget_plan_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.unit_code_from))
            {
                strCriteria = strCriteria + "  And  (unit_code_from ='" + budget_transfer_head.unit_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.budget_code_from))
            {
                strCriteria = strCriteria + "  And  (budget_code_from ='" + budget_transfer_head.budget_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.produce_code_from))
            {
                strCriteria = strCriteria + "  And  (produce_code_from ='" + budget_transfer_head.produce_code_from + "') ";
            }          

            if (!string.IsNullOrEmpty(budget_transfer_head.activity_code_from))
            {
                strCriteria = strCriteria + "  And  (activity_code_from = '" + budget_transfer_head.activity_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.major_code_from))
            {
                strCriteria = strCriteria + "  And  (major_code_from = '" + budget_transfer_head.major_code_from + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.degree_code_to))
            {
                strCriteria = strCriteria + "  And  (degree_code_to ='" + budget_transfer_head.degree_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.budget_plan_code_to))
            {
                strCriteria = strCriteria + "  And  (budget_plan_code_to ='" + budget_transfer_head.budget_plan_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.unit_code_to))
            {
                strCriteria = strCriteria + "  And  (unit_code_to ='" + budget_transfer_head.unit_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.budget_code_to))
            {
                strCriteria = strCriteria + "  And  (budget_code_to ='" + budget_transfer_head.budget_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.produce_code_to))
            {
                strCriteria = strCriteria + "  And  (produce_code_to ='" + budget_transfer_head.produce_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.activity_code_to))
            {
                strCriteria = strCriteria + "  And  (activity_code_to = '" + budget_transfer_head.activity_code_to + "') ";
            }

            if (!string.IsNullOrEmpty(budget_transfer_head.major_code_to))
            {
                strCriteria = strCriteria + "  And  (major_code_to = '" + budget_transfer_head.major_code_to + "') ";
            }

            if (DirectorLock == "Y")
            {
                strCriteria += " and substring(director_code_from,4,2) = substring('" + DirectorCode + "',4,2) ";
                strCriteria += " and substring(director_code_to,4,2) = substring('" + DirectorCode + "',4,2) ";
            }

            #endregion

            strCriteria = strCriteria + " and budget_type ='" + this.BudgetType + "' ";

            try
            {
                if (!oBudget_transfer.SP_BUDGET_TRANSFER_HEAD_SEL(strCriteria, ref ds, ref strMessage))
                {
                    lblError.Text = strMessage;
                }
                else
                {
                    try
                    {
                        GridView1.PageIndex = nPageNo;
                        txthTotalRecord.Value = ds.Tables[0].Rows.Count.ToString();
                        ds.Tables[0].DefaultView.Sort = ViewState["sort"] + " " + ViewState["direction"];
                        GridView1.DataSource = ds.Tables[0];
                        GridView1.DataBind();
                    }
                    catch
                    {
                        GridView1.PageIndex = 0;
                        txthTotalRecord.Value = ds.Tables[0].Rows.Count.ToString();
                        ds.Tables[0].DefaultView.Sort = ViewState["sort"] + " " + ViewState["direction"];
                        GridView1.DataSource = ds.Tables[0];
                        GridView1.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
            finally
            {
                oBudget_transfer.Dispose();
                ds.Dispose();
                if (GridView1.Rows.Count > 0)
                {
                    GridView1.TopPagerRow.Visible = true;
                }
            }
        }