示例#1
0
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (txttypeCode.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนรหัสประเภทพนักงานก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txttypeCode.Focus();
                return;
            }
            if (txttypeName.Text.Trim() == "")
            {
                MessageBox.Show("กรุณาป้อนชื่อประเภทพนักงานก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txttypeName.Focus();
                return;
            }
            foreach (DataGridViewRow row in dgvPerson.Rows)
            {
                string pid = row.Cells["ptCode"].Value.ToString();
                if (txttypeCode.Text.Trim().Equals(pid))
                {
                    MessageBox.Show("รหัสพนักงานนี้มีอยู่แล้ว !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txttypeCode.Focus();
                    return;
                }
            }
            try
            {

                PersonType personel = new PersonType();
                personel.ID = 0;
                personel.ptCode = txttypeCode.Text.Trim();
                personel.ptName = txttypeName.Text.Trim();

                int result = personTypeService.CreatePersonType(personel);
                clear();

                int col = dgvPerson.Columns.Count;
                if (col != 5)
                {
                    ShowDataDefult();
                }
                else
                {
                    showData();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("ไม่สามารถบันทึกได้ " + ex.Message);

            }
        }
        public int DeletePersonType(PersonType _personType)
        {
            int result = -1;
            try
            {
                conn = db.openConn();
                tr = conn.BeginTransaction();

                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" DELETE tbPersonType ");
                sb.Append(" WHERE (ID='" + _personType.ID + "')");
                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 CreatePersonType(PersonType newPersonType)
        {
            int result = -1;
            try
            {
                conn = db.openConn();
                tr = conn.BeginTransaction();
                sb = new StringBuilder();
                sb.Remove(0, sb.Length);
                sb.Append("INSERT INTO tbPersonType(ptCode,ptName)");
                sb.Append(" VALUES (@ptCode,@ptName)");

                string sqlsave;
                sqlsave = sb.ToString();

                comm = new SqlCommand();
                comm.Connection = conn;
                comm.Transaction = tr;
                comm.CommandText = sqlsave;
                comm.Parameters.Clear();
                comm.Parameters.Add("@ptCode", SqlDbType.NVarChar).Value = newPersonType.ptCode;
                comm.Parameters.Add("@ptName", SqlDbType.NVarChar).Value = newPersonType.ptName;
                comm.ExecuteNonQuery();
                tr.Commit();

                result = 1;

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

            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public List<PersonType> getPersonTypeAll()
        {
            PersonType personType = null;
            List<PersonType> personTypes = new List<PersonType>();

            try
            {
                conn = db.openConn();
                sb = new StringBuilder();

                sb.Remove(0, sb.Length);
                sb.Append(" SELECT ID,ptCode,ptName FROM tbPersonType ");

                string sql;
                sql = sb.ToString();

                comm = new SqlCommand();
                comm.CommandText = sql;
                comm.CommandType = CommandType.Text;
                comm.Connection = conn;
                dr = comm.ExecuteReader();
                if (dr.HasRows)
                {

                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    foreach (DataRow drw in dt.Rows)
                    {
                        personType = new PersonType();
                        personType.ID =Convert.ToInt32(drw["ID"].ToString());
                        personType.ptCode = drw["ptCode"].ToString();
                        personType.ptName = drw["ptName"].ToString();
                        personTypes.Add(personType);

                    }

                }

                dr.Close();

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

            }
            finally
            {
                conn.Close();
            }

            return personTypes;
        }
示例#5
0
        private void dgvPerson_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) { return; }
            string linkedit = Convert.ToString(dgvPerson.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            tid = Convert.ToInt32(dgvPerson.Rows[e.RowIndex].Cells["ID"].Value.ToString());
            string tcode = Convert.ToString(dgvPerson.Rows[e.RowIndex].Cells["ptCode"].Value.ToString());
            string tname = Convert.ToString(dgvPerson.Rows[e.RowIndex].Cells["ptName"].Value.ToString());

            if (linkedit.Equals("แก้ไข"))
            {
                txttypeCode.Text = tcode;
                txttypeName.Text = tname;

            }
            else if (linkedit.Equals("ลบ"))
            {
                if (MessageBox.Show("คุณต้องการลบประเภทพนักงาน รหัส : " + tcode + " ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    PersonType personel = new PersonType();
                    personel.ID = tid;
                    int result = personTypeService.DeletePersonType(personel);
                    if (result > -1) {
                        clear();
                        //MessageBox.Show("ลบข้อมูลเรียบร้อย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        showData();
                        tid = 0;
                    }

                }

            }
        }
示例#6
0
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (tid == 0)
            {
                MessageBox.Show("กรุณาป้อนเลือกประเภทพนักงานก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            try
            {

                PersonType personel = new PersonType();
                personel.ID = tid;
                personel.ptCode = txttypeCode.Text.Trim();
                personel.ptName = txttypeName.Text.Trim();
                int result = personTypeService.UpdatePersonType(personel);
                if (result > -1) {
                    int col = dgvPerson.Columns.Count;
                    if (col != 5)
                    {
                        ShowDataDefult();
                    }
                    else
                    {
                        showData();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ไม่สามารถบันทึกได้ " + ex.Message);

            }
        }