protected void btnThem_Click(object sender, EventArgs e) { dv = LayDuLieuTuForm(); cn = new chucnang(); bool exist = cn.CheckMaDonVi(dv.MaDonVi); if (exist) { lblThongBao.Text = "Tác Giả này đã có"; } else { bool result = cn.InsertDonVi(dv); if (result) { lblThongBao.Text = "Thêm thành công"; DoDuLieuVaoGridView(); XoaNoiDung(Page); } else { lblThongBao.Text = "Có lỗi"; } } }
public donvi LayDuLieuTuForm() { donvi dv = new donvi() { MaDonVi = txtMaDonVi.Text, TenDonVi = txtTenDonVi.Text, DiaChi = txtDiaChi.Text, SoDienThoai = txtSoDienThoai.Text, Website = txtWebsite.Text }; return(dv); }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string ma = GridView1.SelectedRow.Cells[0].Text; dv = cn.GetDonViByID(ma); if (dv != null) { txtMaDonVi.Text = dv.MaDonVi; txtTenDonVi.Text = dv.TenDonVi; txtDiaChi.Text = dv.DiaChi; txtSoDienThoai.Text = dv.SoDienThoai; txtWebsite.Text = dv.Website; } }
protected void btnSua_Click(object sender, EventArgs e) { dv = LayDuLieuTuForm(); bool result = cn.UpdateDonVi(dv); if (result) { lblThongBao.Text = "Cập nhập thành công"; DoDuLieuVaoGridView(); XoaNoiDung(Page); } else { lblThongBao.Text = "Có lỗi"; } }
public bool UpdateDonVi(donvi dv) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "update DonVi set TenDV=@ten,DiaChi=@dc,SDT=@sdt,Website=@web where MaDV=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", dv.MaDonVi); cmd.Parameters.AddWithValue("@ten", dv.TenDonVi); cmd.Parameters.AddWithValue("@dc", dv.DiaChi); cmd.Parameters.AddWithValue("@sdt", dv.SoDienThoai); cmd.Parameters.AddWithValue("@web", dv.Website); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
public bool InsertDonVi(donvi dv) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "insert into DonVi values(@ma,@ten,@dc,@sdt,@web)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", dv.MaDonVi); cmd.Parameters.AddWithValue("@ten", dv.TenDonVi); cmd.Parameters.AddWithValue("@dc", dv.DiaChi); cmd.Parameters.AddWithValue("@sdt", dv.SoDienThoai); cmd.Parameters.AddWithValue("@web", dv.Website); conn.Open(); int count = (int)cmd.ExecuteNonQuery(); return(count >= 1); } }
public donvi GetDonViByID(string ma) { using (SqlConnection conn = new SqlConnection(connect)) { string sql = "select * from DonVi where MaDV=@ma"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@ma", ma); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { donvi dv = new donvi() { MaDonVi = (string)dr["MaDV"], TenDonVi = (string)dr["TenDV"], DiaChi = (string)dr["DiaChi"], SoDienThoai = (string)dr["SDT"], Website = (string)dr["Website"] }; return(dv); } return(null); } }