protected void btnThem_Click(object sender, EventArgs e) { int csd; int csc; int dg; csd = Convert.ToInt16(txtChisocdau.Text); csc = Convert.ToInt16(txtChisocuoi.Text); dg = Convert.ToInt16(txtDongia.Text); int tthu; tthu = (csc - csd); txtTieuthu.Text = tthu.ToString(); DN dn = LayDuLieuTuForm(); QLDNDAO QLDAO = new QLDNDAO(); bool exist = QLDAO.checkmact(dn.MaCongToNuoc); if (exist) { lblThongBao.Text = "Mã công tơ nước đã tồn tại"; } else { bool result = QLDAO.Them(dn); if (result) { lblThongBao.Text = "Tính thành công"; LayDuLieuTuGirdview(); } else { lblThongBao.Text = "Lỗi"; } } }
protected void btnSua_Click(object sender, EventArgs e) { int csd; int csc; int dg; csd = Convert.ToInt16(txtChisocdau.Text); csc = Convert.ToInt16(txtChisocuoi.Text); dg = Convert.ToInt16(txtDongia.Text); int tthu; tthu = (csc - csd); txtTieuthu.Text = tthu.ToString(); DN DN = LayDuLieuTuForm(); QLDNDAO QLDAO = new QLDNDAO(); bool result = QLDAO.ChinhSua(DN); if (result) { lblThongBao.Text = "Cập nhật thành công : " + DN.NgayGhi; LayDuLieuTuGirdview(); } else { lblThongBao.Text = "Cập nhật không thành công, vui lòng kiểm tra lại"; } }
public bool Them(DN DN) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"INSERT INTO NUOC(maCongToNuoc,chiSoDau,chiSoCuoi,tieuThu, gia, thanhTien,ngayGhi) VALUES(@mctn,@csd,@csc,@tthu,@Gia,@ttien,@ngayghi)"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@mctn", DN.MaCongToNuoc); cmd.Parameters.AddWithValue("@csd", DN.ChisoDau); cmd.Parameters.AddWithValue("@csc", DN.ChisoCuoi); cmd.Parameters.AddWithValue("@tthu", DN.TieuThu); cmd.Parameters.AddWithValue("@Gia", DN.DonGia); cmd.Parameters.AddWithValue("@ttien", DN.ThanhTien); cmd.Parameters.AddWithValue("@ngayghi", Convert.ToDateTime(DN.NgayGhi)); connection.Open(); int result = cmd.ExecuteNonQuery(); return(result >= 1); } }
public bool ChinhSua(DN DN) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"UPDATE NUOC SET chiSoDau= @csd, chiSoCuoi = @csc, tieuThu = @tthu, gia= @Gia, thanhTien = @ttien, ngayGhi=@ngayghi WHERE maCongToNuoc = @mctn"; SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@mctn", DN.MaCongToNuoc); command.Parameters.AddWithValue("@csd", DN.ChisoDau); command.Parameters.AddWithValue("@csc", DN.ChisoCuoi); command.Parameters.AddWithValue("@tthu", DN.TieuThu); command.Parameters.AddWithValue("@Gia", DN.DonGia); command.Parameters.AddWithValue("@ttien", DN.ThanhTien); command.Parameters.AddWithValue("@ngayghi", DN.NgayGhi); connection.Open(); int result = command.ExecuteNonQuery(); if (result >= 1) { return(true); } } return(false); }
private DN LayDuLieuTuForm() { string maCongToNuoc = txtMacongtonuoc.Text; string chisodau = txtChisocdau.Text; string chisocuoi = txtChisocuoi.Text; string ngayghi = txtNgayghi.Text; float gia = float.Parse(txtDongia.Text); string tieuthu = txtTieuthu.Text; double thanhtien = double.Parse(tieuthu) * gia; long thanhtien1 = Convert.ToInt64(thanhtien); DN dn = new DN { MaCongToNuoc = maCongToNuoc, ChisoDau = chisodau, ChisoCuoi = chisocuoi, NgayGhi = ngayghi, TieuThu = tieuthu, DonGia = gia, ThanhTien = thanhtien1, }; return(dn); }
public DN NAME(string maCongToNuoc) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("SELECT * FROM NUOC WHERE maCongToNuoc = @mn ", connection); cmd.Parameters.AddWithValue("@mn", maCongToNuoc); connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { DN NC = new DN { MaCongToNuoc = (string)reader["maCongToNuoc"], ChisoDau = (string)reader["chiSoDau"], ChisoCuoi = (string)reader["chiSoCuoi"], DonGia = (float)reader["gia"], ThanhTien = (long)reader["thanhTien"], NgayGhi = reader["ngayGhi"].ToString(), }; return(NC); } return(null); } }