public loaisach LayDuLieuTuForm() { loaisach ls = new loaisach() { MaLoai = txtMaLoaiSach.Text, TenLoaiSach = txtTenLoaiSach.Text }; return(ls); }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string maLoai = GridView1.SelectedRow.Cells[0].Text; ls = cn.GetLoaiSachByID(maLoai); if (ls != null) { txtMaLoaiSach.Text = ls.MaLoai; txtTenLoaiSach.Text = ls.TenLoaiSach; } }
public bool UpdateLoaiSach(loaisach ls) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "update LoaiSach set TenLoaiSach=@ten where MaLoai=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", ls.MaLoai); cmd.Parameters.AddWithValue("@ten", ls.TenLoaiSach); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
public bool InsertLoaiSach(loaisach ls) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "insert into LoaiSach values(@ma,@ten)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", ls.MaLoai); cmd.Parameters.AddWithValue("@ten", ls.TenLoaiSach); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
protected void btnSua_Click(object sender, EventArgs e) { ls = LayDuLieuTuForm(); bool result = cn.UpdateLoaiSach(ls); if (result) { lblThongBao.Text = "Cập nhập thành công"; DoDuLieuVaoGridView(); } else { lblThongBao.Text = "Có lỗi"; } }
public loaisach GetLoaiSachByID(string maLoai) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "select * from LoaiSach where MaLoai=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", maLoai); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { loaisach ls = new loaisach() { MaLoai = (string)dr["MaLoai"], TenLoaiSach = (string)dr["TenLoaiSach"] }; return(ls); } return(null); } }
protected void btnThem_Click(object sender, EventArgs e) { ls = LayDuLieuTuForm(); cn = new chucnang(); bool exist = cn.CheckMaLoaiSach(ls.MaLoai); if (exist) { lblThongBao.Text = "Tác Giả này đã có"; } else { bool result = cn.InsertLoaiSach(ls); if (result) { lblThongBao.Text = "Thêm thành công"; DoDuLieuVaoGridView(); } else { lblThongBao.Text = "Có lỗi"; } } }