Exemple #1
0
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (txtamount.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนจำนวนโบนัส ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtamount.Focus();
                return;
            }

            Bonus _bonus = new Bonus();

            Person _p = new Person();
            _p.psnCode = txtpsnCode.Text.Trim();
            _bonus.person = _p;
            _bonus.amount =  Convert.ToInt32(txtamount.Text.Trim());
            _bonus.bdate = DateTime.Now;

            int result = bonusService.CreateBonus(_bonus);
            if (result > -1)
            {
                Console.WriteLine("Insert Complete");
                lblresult.Visible = true;
                lblresult.Text = " บันทึกเรียบร้อย ";
                //MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                Console.WriteLine("Insert Not Complete");
                lblresult.Visible = true;
                lblresult.Text = " ไม่สามารถบันทึกข้อมูลได้";
                //MessageBox.Show("ไม่สามารถบันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public int UpdateBonus(Bonus _bonus)
        {
            int result = -1;
            try
            {
                conn = db.openConn();
                tr = conn.BeginTransaction();

                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" UPDATE tbBonus ");
                sb.Append(" SET amount='" + _bonus.amount + "',");
                sb.Append(" bdate='" + _bonus.bdate + "'");
                sb.Append(" WHERE (psnCode='" + _bonus.person.psnCode + "')");
                string sqlUpdate;
                sqlUpdate = sb.ToString();

                comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = sqlUpdate;
                comm.Transaction = tr;
                comm.Parameters.Clear();
                comm.ExecuteNonQuery();
                tr.Commit();

                result = 1;

            }
            catch (Exception ex)
            {
                tr.Rollback();
                conn.Close();
                return result;
                throw ex;

            }
            finally
            {
                conn.Close();
            }

            return result;
        }
        public int CreateBonus(Bonus newBonus)
        {
            int result = -1;
            try
            {
                conn = db.openConn();
                tr = conn.BeginTransaction();
                sb = new StringBuilder();
                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO tbBonus(psnCode,amount,bdate)");
                sb.Append(" VALUES (@psnCode,@amount,@bdate)");

                string sqlsave;
                sqlsave = sb.ToString();

                comm = new SqlCommand();
                comm.Connection = conn;
                comm.Transaction = tr;
                comm.CommandText = sqlsave;
                comm.Parameters.Clear();
                comm.Parameters.Add("@psnCode", SqlDbType.NVarChar).Value = newBonus.person.psnCode;
                comm.Parameters.Add("@amount", SqlDbType.NVarChar).Value = newBonus.amount;
                comm.Parameters.Add("@bdate", SqlDbType.DateTime).Value = newBonus.bdate;
                comm.ExecuteNonQuery();
                tr.Commit();

                result = 1;

            }
            catch (Exception ex)
            {
                tr.Rollback();
                conn.Close();
                return result;
                throw ex;

            }
            finally
            {
                conn.Close();
            }
            return result;
        }