protected void btnThem_Click(object sender, EventArgs e) { if (txtMaKH.Text != "" && txtHoTen.Text != "" && txtDienThoai.Text != "" && txtDiaChi.Text != "") { // Lấy các giá trị từ giao diện KH kH = LayDuLieuTuForm(); KhachHangDAO khachHangDAO = new KhachHangDAO(); // Kiểm tra username này đã tồn tại trong CSDL chưa bool exist = khachHangDAO.CheckMaKH(kH.MaKH); if (exist) { lblMessage.Text = "khách hàng đã tồn tại"; } else { // Thực hiện ghi xuống CSDL bool result = khachHangDAO.Insert(kH); if (result) { lblMessage.Text = "Thêm thành công!"; LayDuLieuVaoGridView(); } else { lblMessage.Text = "Có lỗi. Vui lòng thử lại sau"; } } } else { lblMessage.Text = "Vui lòng điền đầy đủ thông tin"; } }
private void DoDuLieuLenForm(KH kH) { txtMaKH.Text = kH.MaKH; txtHoTen.Text = kH.HoTen; txtDiaChi.Text = kH.DiaChi; txtDienThoai.Text = kH.DienThoai; }
public KH GetTheLoaiByMaKH(string makh) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"SELECT * FROM KhachHang WHERE MaKH = @makh"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@makh", makh); connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { KH kH = new KH { // Lấy giá trị theo tên cột trong CSDL, MaKH = (string)reader["MaKH"], HoTen = (string)reader["HoTen"], DiaChi = (string)reader["DiaChi"], DienThoai = (string)reader["DienThoai"] }; return(kH); } } return(null); }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string makh = GridView1.SelectedRow.Cells[0].Text; KhachHangDAO khachHangDAO = new KhachHangDAO(); KH kH = khachHangDAO.GetTheLoaiByMaKH(makh); if (kH != null) { // Đổ dữ liệu từ đối tượng TheLoai vào các trường trên Form DoDuLieuLenForm(kH); } }
private KH LayDuLieuTuForm() { string makh = txtMaKH.Text; string hoten = txtHoTen.Text; string diachi = txtDiaChi.Text; string dienthoai = txtDienThoai.Text; KH kH = new KH { MaKH = makh, HoTen = hoten, DiaChi = diachi, DienThoai = dienthoai }; return(kH); }
public bool Insert(KH kH) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"INSERT INTO KhachHang(MaKH,HoTen,DiaChi,DienThoai) VALUES (@makh,@hoten,@diachi,@dienthoai)"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@makh", kH.MaKH); cmd.Parameters.AddWithValue("@hoten", kH.HoTen); cmd.Parameters.AddWithValue("@diachi", kH.DiaChi); cmd.Parameters.AddWithValue("@dienthoai", kH.DienThoai); connection.Open(); int result = cmd.ExecuteNonQuery(); return(result >= 1); } }
protected void btnSua_Click(object sender, EventArgs e) { if (txtMaKH.Text != "" && txtHoTen.Text != "" && txtDienThoai.Text != "" && txtDiaChi.Text != "") { KH kH = LayDuLieuTuForm(); KhachHangDAO khachHangDAO = new KhachHangDAO(); bool result = khachHangDAO.UpdateKhachHang(kH); if (result) { lblMessage.Text = "Cập nhật khách hàng thành công"; LayDuLieuVaoGridView(); } else { lblMessage.Text = "Cập nhật không thành công, vui lòng kiểm tra lại"; } } else { lblMessage.Text = "Vui lòng điền đầy đủ thông tin"; } }
public bool UpdateKhachHang(KH kH) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"UPDATE KhachHang SET HoTen = @hoten, DiaChi = @diachi, DienThoai = @dienthoai WHERE MaKH = @makh"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@makh", kH.MaKH); cmd.Parameters.AddWithValue("@hoten", kH.HoTen); cmd.Parameters.AddWithValue("@diachi", kH.DiaChi); cmd.Parameters.AddWithValue("@dienthoai", kH.DienThoai); connection.Open(); int result = cmd.ExecuteNonQuery(); if (result >= 1) { return(true); } } return(false); }