Exemple #1
0
        public static void ThemTienCong(TIENCONG_DTO tc)
        {
            SqlConnection connection = new SqlConnection();

            connection.ConnectionString = KetNoi.Instance.conn;
            connection.Open();
            SqlCommand cmd = new SqlCommand("ThemTienCong", connection);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenTienCong", SqlDbType.NVarChar, 20);
            cmd.Parameters.Add("@TienCong", SqlDbType.Int);
            cmd.Parameters["@TenTienCong"].Value = tc.TenTienCong;
            cmd.Parameters["@TienCong"].Value    = tc.TienCong;
            int count = cmd.ExecuteNonQuery();

            if (count > 0)
            {
                MessageBox.Show("Thêm thành công");
            }
            connection.Close();
        }
        // sự kiện nhấn nút lưu
        private void btnLuu_Click(object sender, EventArgs e)
        {
            string ten;
            int    tien;

            if (btnNew.Enabled || btnUpdate.Enabled)
            {
                if (txtTenTienCong.Text.ToString() == "")
                {
                    MessageBox.Show("Chưa nhập tên tiền công");
                }
                else
                {
                    ten = Convert.ToString(txtTenTienCong.Text);
                }
                if (txtTienCong.Text != null)
                {
                    int n = 0;
                    if (int.TryParse(this.txtTienCong.Text, out n))
                    {
                        tien = Convert.ToInt32(txtTienCong.Text);
                        TIENCONG_DTO tc  = new TIENCONG_DTO(txtTenTienCong.Text, tien);
                        TIENCONG_DTO tc1 = new TIENCONG_DTO(Convert.ToInt32(txtMaTienCong.Text), txtTenTienCong.Text, tien);
                        if (btnNew.Enabled)
                        {
                            DialogResult dr;
                            dr = MessageBox.Show("Bạn có muốn thêm loại tiền công '" + txtTenTienCong.Text + "' hay không ?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                            if (dr == DialogResult.OK)
                            {
                                TIENCONG_DAO.ThemTienCong(tc);
                            }
                            dgv_load();
                            dgvTienCong.CurrentCell         = dgvTienCong.Rows[dgvTienCong.Rows.Count - 2].Cells[0];
                            dgvTienCong.CurrentRow.Selected = true;
                            txtMaTienCong.Text  = Convert.ToString(dgvTienCong.Rows[dgvTienCong.Rows.Count - 2].Cells["MaTienCong"].Value);
                            txtTenTienCong.Text = Convert.ToString(dgvTienCong.Rows[dgvTienCong.Rows.Count - 2].Cells["TenTienCong"].Value);
                            txtTienCong.Text    = Convert.ToString(dgvTienCong.Rows[dgvTienCong.Rows.Count - 2].Cells["TienCong"].Value);
                        }
                        if (btnUpdate.Enabled)
                        {
                            DialogResult dr;
                            dr = MessageBox.Show("Bạn có muốn cập nhật loại tiền công '" + txtTenTienCong.Text + "' hay không ?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                            if (dr == DialogResult.OK)
                            {
                                TIENCONG_DAO.CapNhatTienCong(tc1);
                            }
                            dgv_load();
                            dgvTienCong.CurrentCell         = dgvTienCong.Rows[ChiMuc].Cells[0];
                            dgvTienCong.CurrentRow.Selected = true;
                            txtMaTienCong.Text  = Convert.ToString(dgvTienCong.Rows[ChiMuc].Cells["MaTienCong"].Value);
                            txtTenTienCong.Text = Convert.ToString(dgvTienCong.Rows[ChiMuc].Cells["TenTienCong"].Value);
                            txtTienCong.Text    = Convert.ToString(dgvTienCong.Rows[ChiMuc].Cells["TienCong"].Value);
                        }
                    }
                    else
                    {
                        if (txtTienCong.Text.ToString() != "")
                        {
                            MessageBox.Show("Số tiền phải là ký số");
                        }
                    }
                }
                btnSave.Enabled         = false;
                btnNew.Enabled          = true;
                btnNew.Enabled          = true;
                txtMaTienCong.ReadOnly  = true;
                txtTenTienCong.ReadOnly = true;
                txtTienCong.ReadOnly    = true;
            }
        }