private void DMHang_Load(object sender, EventArgs e) { DAO.OpenConnection(); LoadDataToGridView(); fillDataToCombo(); DAO.CloseConnetion(); }
private void btnXoa_Click(object sender, EventArgs e) { string sql = "delete from tblchatlieu where machatlieu = '" + txtMachatLieu.Text + "'"; SqlCommand cmd = new SqlCommand(); DAO.OpenConnection(); cmd.CommandText = sql; cmd.Connection = DAO.conn; cmd.ExecuteNonQuery(); DAO.CloseConnetion(); LoadDataToGrivew(); }
private void btnLuu_Click(object sender, EventArgs e) { // kiểm tra dữ liệu //- Các trường ko được trống if (txtMaHang.Text == "") { MessageBox.Show("Bạn không được để trống mã hàng"); txtMaHang.Focus(); return; } if (txtTenHang.Text == "") { MessageBox.Show("Bạn không được để trống tên hàng"); txtTenHang.Focus(); return; } if (cmbChatLieu.SelectedIndex == -1) { MessageBox.Show("Bạn chưa chọn chất liệu"); return; } // tương tự check them cho những texbox còn lại // - Mã hàng ko được trùng string sql = "select * from tblHang where Mahang = '" + txtMaHang.Text.Trim() + "'"; DAO.OpenConnection(); if (DAO.checkKeyExit(sql)) { MessageBox.Show("mã hàng đã tồn tại"); txtMaHang.Focus(); DAO.CloseConnetion(); return; } else { sql = "insert into tblHang (mahang, tenhang, dongianhap,dongiaban, " + "soluong, machatlieu, anh, ghichu)" + " values ('" + txtMaHang.Text.Trim() + "',N'" + txtTenHang.Text.Trim() + "', " + txtGiaNhap.Text + "," + txtGiaBan.Text.Trim() + "," + txtSoLuong.Text.Trim() + ",'" + cmbChatLieu.SelectedValue.ToString() + "', null, null)"; SqlCommand cmd = new SqlCommand(sql, DAO.conn); MessageBox.Show(sql); cmd.ExecuteNonQuery(); LoadDataToGridView(); fillDataToCombo(); DAO.CloseConnetion(); } }
private void btnSua_Click(object sender, EventArgs e) { string Sql = "Update tblChatLieu set TenChatLieu = N'" + txtTenChatLieu.Text.Trim() + "' where MaChatLieu = '" + txtMachatLieu.Text + "'"; DAO.OpenConnection(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = Sql; cmd.Connection = DAO.conn; cmd.ExecuteNonQuery(); DAO.CloseConnetion(); LoadDataToGrivew(); }
private void LoadDataToGrivew() { //mở kết nối try { DAO.OpenConnection(); string sql = "select * from tblchatlieu"; SqlDataAdapter myAdapter = new SqlDataAdapter(sql, DAO.conn); DataTable tblChatLieu = new DataTable(); myAdapter.Fill(tblChatLieu); gridviewChatLieu.DataSource = tblChatLieu; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { DAO.CloseConnetion(); } }
private void btnLuu_Click(object sender, EventArgs e) { // kiem tra dieu kien if (txtMachatLieu.Text == "") { MessageBox.Show("bạn chưa nhập mã chất liệu "); txtMachatLieu.Focus(); return; } if (txtTenChatLieu.Text == "") { MessageBox.Show("bạn chưa nhập tên chất liệu"); txtTenChatLieu.Focus(); return; } string sqlCheckKey = "SElect * from tblChatLieu Where machatlieu = '" + txtMachatLieu.Text.Trim() + "'"; DAO.OpenConnection(); if (DAO.checkKeyExit(sqlCheckKey)) { MessageBox.Show("Mã chất liệu đã tồn tại"); DAO.CloseConnetion(); txtMachatLieu.Focus(); return; } else { string sql = "insert into tblChatLieu values ('" + txtMachatLieu.Text.Trim() + "' , N'" + txtTenChatLieu.Text.Trim() + "')"; SqlCommand cmd = new SqlCommand(sql, DAO.conn); cmd.ExecuteNonQuery(); DAO.CloseConnetion(); LoadDataToGrivew(); } }