Esempio n. 1
0
 public bool Update(Supiler updateSupiler)
 {
     int TempId = ds.UpdateSupiler(updateSupiler);
     if (TempId > 0)
     {
         return true;
     }
     return false;
 }
Esempio n. 2
0
 public bool Save(Supiler newSupiler)
 {
     int TempId = ds.CreateSupiler(newSupiler);
     if (TempId > 0)
     {
         return true;
     }
     return false;
 }
        public int CreateSupiler(Supiler newSupiler)
        {
            int result = -1;
            try
            {
                Conn = db.openConnAccess();
                tr = Conn.BeginTransaction();

                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO tbSupiler(SPCode,SPName,Address,PersonContact,Phone,Fax)");
                sb.Append(" VALUES (@SPCode,@SPName,@Address,@PersonContact,@Phone,@Fax)");

                string sqlSave;
                sqlSave = sb.ToString();

                com = new OleDbCommand();
                com.Connection = Conn;
                com.CommandText = sqlSave;
                com.Transaction = tr;
                com.Parameters.Clear();
                com.Parameters.Add("@SPCode", OleDbType.VarChar).Value = newSupiler.SPCode;
                com.Parameters.Add("@SPName", OleDbType.VarChar).Value = newSupiler.SPName;
                com.Parameters.Add("@Address", OleDbType.VarChar).Value = newSupiler.Address;
                com.Parameters.Add("@PersonContact", OleDbType.VarChar).Value = newSupiler.PersonContact;
                com.Parameters.Add("@Phone", OleDbType.VarChar).Value = newSupiler.Phone;
                com.Parameters.Add("@Fax", OleDbType.VarChar).Value = newSupiler.Fax;
                com.ExecuteNonQuery();
                tr.Commit();

                result = 1;

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

            }
            finally
            {
                Conn.Close();
            }

            return result;
        }
        public IList<Supiler> findSupilerByCodeOrName(string _name)
        {
            IList<Supiler> supilers = new List<Supiler>();
            Supiler supiler = null;

            try
            {
                Conn = db.openConnAccess();
                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" SELECT ID,SPCode,SPName,Address,PersonContact,Phone,Fax FROM tbSupiler ");
                sb.Append(" WHERE 1=1 ");
                sb.Append(" AND SPCode='" + _name + "'");
                sb.Append(" OR SPName LIKE '%" + _name + "%'");
                sb.Append(" order by ID desc");

                string sql;
                sql = sb.ToString();

                com = new OleDbCommand();
                com.CommandText = sql;
                com.CommandType = CommandType.Text;
                com.Connection = Conn;
                dr = com.ExecuteReader();
                if (dr.HasRows)
                {
                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    int index = 1;
                    foreach (DataRow drw in dt.Rows)
                    {
                        supiler = new Supiler();
                        supiler.ID = Convert.ToInt32(drw["ID"].ToString());
                        supiler.Index = Convert.ToString(index);
                        supiler.SPCode = drw["SPCode"].ToString();
                        supiler.SPName = drw["SPName"].ToString();
                        supiler.Address = drw["Address"].ToString();
                        supiler.PersonContact = drw["PersonContact"].ToString();
                        supiler.Phone = drw["Phone"].ToString();
                        supiler.Fax = drw["Fax"].ToString();
                        supilers.Add(supiler);
                        index++;

                    }

                }

                dr.Close();

            }
            catch (Exception ex)
            {
                dr.Close();
                Conn.Close();
                return null;
                throw ex;

            }
            finally
            {
                Conn.Close();
            }

            return supilers;
        }
        public int UpdateSupiler(Supiler updateSupiler)
        {
            int result = -1;
            try
            {
                Conn = db.openConnAccess();
                tr = Conn.BeginTransaction();

                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" UPDATE tbSupiler ");
                sb.Append(" SET SPName='" + updateSupiler.SPName +"',");
                sb.Append(" Address='" + updateSupiler.Address + "',");
                sb.Append(" PersonContact='" + updateSupiler.PersonContact + "',");
                sb.Append(" Phone='" + updateSupiler.Phone + "',");
                sb.Append(" Fax='" + updateSupiler.Fax + "'");
                sb.Append(" WHERE (SPCode='" + updateSupiler.SPCode + "')");
                string sqlUpdate;
                sqlUpdate = sb.ToString();

                com = new OleDbCommand();
                com.Connection = Conn;
                com.CommandText = sqlUpdate;
                com.Transaction = tr;
                com.Parameters.Clear();
                com.ExecuteNonQuery();
                tr.Commit();

                result = 1;

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

            }
            finally
            {
                Conn.Close();
            }

            return result;
        }
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (txtSupilerCode.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนรหัสผู้จัดจำหน่ายก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtSupilerCode.Focus();
                return;
            }
            if (txtSupilerName.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนชื่อผู้จัดจำหน่ายก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtSupilerName.Focus();
                return;
            }
            if (txtSupilerAddress.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนชื่อที่อยู่ผู้จัดจำหน่ายก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtSupilerAddress.Focus();
                return;
            }
            if (txtContact.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนชื่อผู้ติดต่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtContact.Focus();
                return;
            }
            if (txtPhone.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนโทรศัพท์ก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPhone.Focus();
                return;
            }

            if (MessageBox.Show("คุณต้องการเพิ่มผู้จัดจำหน่ายใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {

                    Supiler supiler = serviceSupiler.getByCode(txtSupilerCode.Text.Trim());
                    if (supiler == null)
                    {
                        Supiler newSupiler = new Supiler();
                        newSupiler.SPCode = txtSupilerCode.Text.Trim();
                        newSupiler.SPName = txtSupilerName.Text.Trim();
                        newSupiler.Address = txtSupilerAddress.Text.Trim();
                        newSupiler.PersonContact = txtContact.Text.Trim();
                        newSupiler.Phone = txtPhone.Text.Trim();
                        newSupiler.Fax = txtFax.Text.Trim();

                        bool save = serviceSupiler.Save(newSupiler);
                        if (save)
                        {
                            MessageBox.Show("เพิ่มผู้จัดจำหน่าย เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            loadData();
                        }
                        else
                        {
                            MessageBox.Show("ไม่สามารถ เพิ่มมผู้จัดจำหน่าย ใหม่ได้!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }
                    }
                    else
                    {

                        MessageBox.Show("มีมผู้จัดจำหน่าย นี้อยู่แล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("ไม่สามารถ เพิ่มมผู้จัดจำหน่าย ได้ เนื่องจาก !!! : " + ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }