Esempio n. 1
0
        private bool saveData()
        {
            bool   blnResult      = false;
            bool   blnDup         = false;
            string strMessage     = string.Empty;
            string strbranch_code = string.Empty,
                   strbranch_name = string.Empty,
                   strbank_code   = string.Empty,
                   stractive      = string.Empty,
                   strcreatedby   = string.Empty,
                   strupdatedby   = string.Empty;
            string  strScript     = string.Empty;
            cBranch oBranch       = new cBranch();
            DataSet ds            = new DataSet();

            try
            {
                #region set Data
                strbranch_code = txtbranch_code.Text;
                strbranch_name = txtbranch_name.Text;
                //Bank_code
                strbank_code = cboBank.SelectedValue;
                if (Request.Form["ctl00$ASPxRoundPanel1$ContentPlaceHolder1$cboBank"] != null)
                {
                    strbank_code = Request.Form["ctl00$ASPxRoundPanel1$ContentPlaceHolder1$cboBank"].ToString();
                }
                if (chkStatus.Checked == true)
                {
                    stractive = "Y";
                }
                else
                {
                    stractive = "N";
                }
                strcreatedby = Session["username"].ToString();
                strupdatedby = Session["username"].ToString();
                #endregion

                string strCheckAdd = " and branch.branch_code = '" + strbranch_code.Trim() + "' ";
                if (!oBranch.SP_SEL_BRANCH(strCheckAdd, ref ds, ref strMessage))
                {
                    lblError.Text = strMessage;
                }
                else
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        #region check dup
                        string strCheckDup = string.Empty;
                        strCheckDup = " and branch.branch_name = '" + strbranch_name.Trim() + "' " +
                                      " and branch.bank_code='" + strbank_code.Trim() + "'  and  branch.branch_code <> '" + strbranch_code.Trim() + "' ";
                        if (!oBranch.SP_SEL_BRANCH(strCheckDup, ref ds, ref strMessage))
                        {
                            lblError.Text = strMessage;
                        }
                        else
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                strScript =
                                    "alert(\"ไม่สามารถแก้ไขข้อมูลได้ เนื่องจาก" +
                                    "\\nข้อมูลธนาคาร : " + cboBank.SelectedItem.Text +
                                    "\\nข้อมูลสาขา : " + strbranch_name.Trim() +
                                    "\\nซ้ำ\");\n";
                                blnDup = true;
                            }
                        }
                        #endregion
                        #region edit
                        if (!blnDup)
                        {
                            if (oBranch.SP_UPD_BRANCH(strbranch_code, strbranch_name, strbank_code, stractive, strupdatedby, ref strMessage))
                            {
                                blnResult = true;
                            }
                            else
                            {
                                lblError.Text = strMessage.ToString();
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "chkdup", strScript, true);
                        }
                        #endregion
                    }
                    else
                    {
                        #region check dup
                        string strCheckDup = string.Empty;
                        strCheckDup = " and branch.branch_name = '" + strbranch_name + "' " +
                                      " and branch.bank_code='" + strbank_code.Trim() + "' ";
                        if (!oBranch.SP_SEL_BRANCH(strCheckDup, ref ds, ref strMessage))
                        {
                            lblError.Text = strMessage;
                        }
                        else
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                strScript =
                                    "alert(\"ไม่สามารถเพิ่มข้อมูลได้ เนื่องจาก" +
                                    "\\nข้อมูลธนาคาร: " + cboBank.SelectedItem.Text +
                                    "\\nข้อมูลสาขา : " + strbranch_name.Trim() +
                                    "\\nซ้ำ\");\n";
                                blnDup = true;
                            }
                        }
                        #endregion
                        #region insert
                        if (!blnDup)
                        {
                            if (oBranch.SP_INS_BRANCH(strbranch_name, strbank_code, stractive, strcreatedby, ref strMessage))
                            {
                                string strGetcode = " and branch.branch_name = '" + strbranch_name.Trim() + "' " +
                                                    " and branch.bank_code = '" + strbank_code.Trim() + "' ";
                                if (!oBranch.SP_SEL_BRANCH(strGetcode, ref ds, ref strMessage))
                                {
                                    lblError.Text = strMessage;
                                }
                                if (ds.Tables[0].Rows.Count > 0)
                                {
                                    strbranch_code = ds.Tables[0].Rows[0]["branch_code"].ToString();
                                }
                                ViewState["branch_code"] = strbranch_code;
                                blnResult = true;
                            }
                            else
                            {
                                lblError.Text = strMessage.ToString();
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "close", strScript, true);
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
            finally
            {
                oBranch.Dispose();
            }
            return(blnResult);
        }