public sach GetSachByID(string maSach) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "select * from Sach where MaSach=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", maSach); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { sach s = new sach() { MaSach = (string)dr["MaSach"], TenSach = (string)dr["TenSach"], MaNhaXuatBan = (string)dr["MaNhaXB"], MoTa = (string)dr["MoTa"], MaLoai = (string)dr["MaLoai"], MaTacGia = (string)dr["MaTacGia"], SoLuong = (int)dr["SoLuong"] }; return(s); } return(null); } }
protected void btnSua_Click(object sender, EventArgs e) { s = LayDuLieuTuForm(); bool result = cn.UpdateSach(s); if (result) { lblThongBao.Text = "Cập nhập thành công"; DoDuLieuVaoGridView(); } else { lblThongBao.Text = "Có lỗi"; } }
public sach LayDuLieuTuForm() { sach s = new sach() { MaSach = txtMaSach.Text, TenSach = txtTenSach.Text, MaNhaXuatBan = ddlNhaXuatBan.SelectedValue.ToString(), MoTa = txtMoTa.Text, MaLoai = ddlLoaiSach.SelectedValue.ToString(), MaTacGia = ddlTacGia.SelectedValue.ToString(), SoLuong = int.Parse(txtSoLuong.Text) }; return(s); }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string maSach = GridView1.SelectedRow.Cells[0].Text; s = cn.GetSachByID(maSach); if (s != null) { txtMaSach.Text = s.MaSach; txtTenSach.Text = s.TenSach; ddlNhaXuatBan.SelectedValue = s.MaNhaXuatBan; txtMoTa.Text = s.MoTa; ddlLoaiSach.SelectedValue = s.MaLoai; ddlTacGia.SelectedValue = s.MaTacGia; txtSoLuong.Text = s.SoLuong.ToString(); } }
public bool UpdateSach(sach s) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "update Sach set TenSach=@ten,MaNhaXB=@manxb,MoTa=@mt,MaLoai=@maloai,MaTacGia=@matacgia,SoLuong=@sl where MaSach=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", s.MaSach); cmd.Parameters.AddWithValue("@ten", s.TenSach); cmd.Parameters.AddWithValue("@manxb", s.MaNhaXuatBan); cmd.Parameters.AddWithValue("@mt", s.MoTa); cmd.Parameters.AddWithValue("@maloai", s.MaLoai); cmd.Parameters.AddWithValue("@matacgia", s.MaTacGia); cmd.Parameters.AddWithValue("@sl", s.SoLuong); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
public bool InsertSach(sach s) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "insert into Sach values(@ma,@ten,@manxb,@mota,@maloai,@matacgia,@sl)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", s.MaSach); cmd.Parameters.AddWithValue("@ten", s.TenSach); cmd.Parameters.AddWithValue("@manxb", s.MaNhaXuatBan); cmd.Parameters.AddWithValue("@mota", s.MoTa); cmd.Parameters.AddWithValue("@maloai", s.MaLoai); cmd.Parameters.AddWithValue("@matacgia", s.MaTacGia); cmd.Parameters.AddWithValue("@sl", s.SoLuong); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
protected void btnThem_Click(object sender, EventArgs e) { s = LayDuLieuTuForm(); cn = new chucnang(); bool exist = cn.CheckMaSach(s.MaSach); if (exist) { lblThongBao.Text = "Tác Giả này đã có"; } else { bool result = cn.InsertSach(s); if (result) { lblThongBao.Text = "Thêm thành công"; DoDuLieuVaoGridView(); } else { lblThongBao.Text = "Có lỗi"; } } }