private void cmdAdd_Click(object sender, EventArgs e) { if (txtpcode.Text.Trim() == "") { MessageBox.Show("กรุณาป้อนรหัสตำแหน่งก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtpcode.Focus(); return; } if (txtpname.Text.Trim() == "") { MessageBox.Show("กรุณาป้อนชื่อตำแหน่งก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtpname.Focus(); return; } foreach (DataGridViewRow row in dgvposition.Rows) { string pid = row.Cells["PCode"].Value.ToString(); if (txtpcode.Text.Trim().Equals(pid)) { MessageBox.Show("รหัสตำแหน่งนี้มีอยู่แล้ว !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtpcode.Focus(); return; } } try { Position position = new Position(); position.ID = 0; position.psCode = txtpcode.Text.Trim(); position.psName = txtpname.Text.Trim(); int result = positonService.CreatePosition(position); if (result > -1) { clear(); int col = dgvposition.Columns.Count; if (col != 5) { ShowDataDefult(); } else { Showdata(); } } } catch (Exception ex) { MessageBox.Show("ไม่สามารถบันทึกได้ " + ex.Message); } }
public int DeletePosition(Position _position) { int result = -1; try { conn = db.openConn(); tr = conn.BeginTransaction(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append(" DELETE tbPosition "); sb.Append(" WHERE (ID='" + _position.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 CreatePosition(Position newPosition) { int result = -1; try { conn = db.openConn(); tr = conn.BeginTransaction(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append("INSERT INTO tbPosition(psCode,psName)"); sb.Append(" VALUES (@psCode,@psName)"); string sqlsave; sqlsave = sb.ToString(); comm = new SqlCommand(); comm.Connection = conn; comm.Transaction = tr; comm.CommandText = sqlsave; comm.Parameters.Clear(); comm.Parameters.Add("@psCode", SqlDbType.NVarChar).Value = newPosition.psCode; comm.Parameters.Add("@psName", SqlDbType.NVarChar).Value = newPosition.psName; comm.ExecuteNonQuery(); tr.Commit(); result = 1; } catch (Exception ex) { tr.Rollback(); conn.Close(); return result; throw ex; } finally { conn.Close(); } return result; }
private void dgvposition_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) { return; } string linkedit = Convert.ToString(dgvposition.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()); tid = Convert.ToInt32(dgvposition.Rows[e.RowIndex].Cells["ID"].Value.ToString()); string tcode = Convert.ToString(dgvposition.Rows[e.RowIndex].Cells["psCode"].Value.ToString()); string tname = Convert.ToString(dgvposition.Rows[e.RowIndex].Cells["psName"].Value.ToString()); if (linkedit.Equals("แก้ไข")) { txtpcode.Text = tcode; txtpname.Text = tname; } else if (linkedit.Equals("ลบ")) { if (MessageBox.Show("คุณต้องการลบตำแหน่ง รหัส : " + tcode + " ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { Position position = new Position(); position.ID = tid; int result = positonService.DeletePosition(position); if (result > -1) { clear(); Showdata(); tid = 0; } } } }
private void cmdSave_Click(object sender, EventArgs e) { if (tid == 0) { MessageBox.Show("กรุณาป้อนเลือกรหัสตำแหน่งก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } try { Position position = new Position(); position.ID = tid; position.psCode = txtpcode.Text.Trim(); position.psName = txtpname.Text.Trim(); int result = positonService.UpdatePosition(position); if (result > -1) { int col = dgvposition.Columns.Count; if (col != 5) { ShowDataDefult(); } else { Showdata(); } } } catch (Exception ex) { MessageBox.Show("ไม่สามารถบันทึกได้ " + ex.Message); } }
public List<Position> getPositionAll() { Position position = null; List<Position> positions = new List<Position>(); try { conn = db.openConn(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append(" SELECT ID,psCode,psName FROM tbPosition "); 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) { position = new Position(); position.ID = Convert.ToInt32(drw["ID"].ToString()); position.psCode = drw["psCode"].ToString(); position.psName = drw["psName"].ToString(); positions.Add(position); } } dr.Close(); } catch (Exception ex) { dr.Close(); conn.Close(); return null; throw ex; } finally { conn.Close(); } return positions; }