asp.linh
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Bton
{
public partial class hososv : System.Web.UI.Page
{
public SqlConnection cnn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\QLSV.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
adddata();
}
}
public void adddata()
{
lstsv.Items.Clear();
SqlCommand cmd = new SqlCommand("Select MASV from SINHVIEN",cnn);
SqlDataReader read;
try
{
cnn.Open();
read = cmd.ExecuteReader();
while (read.Read())
{
lstsv.Items.Add(read["MASV"].ToString());
}
read.Close();
}
catch (Exception err)
{
lblthongbao.Text = err.Message;
}
finally
{
cnn.Close();
}
}
protected void lstsv_SelectedIndexChanged(object sender, EventArgs e)
{
lblthongbao.Text = "";
SqlCommand cmd = new SqlCommand("Select * from SINHVIEN Where MASV='" + lstsv.SelectedItem.Value + "'", cnn);
SqlDataReader read;
try
{
cnn.Open();
read = cmd.ExecuteReader();
read.Read();
txtmasv.Text = read["MASV"].ToString();
txthoten.Text = read["TENSV"].ToString();
txtgioitinh.Text = read["GIOITINH"].ToString();
txtngaysinh.Text = read["NGAYSINH"].ToString();
txtdiachi.Text = read["DIACHI"].ToString();
read.Close();
}
catch (Exception err)
{
lblthongbao.Text = "Lỗi." + "<br>";
lblthongbao.Text += err.Message;
}
finally
{
cnn.Close();
}
}
protected void btnthem_Click(object sender, EventArgs e)
{
lblthongbao.Text = "";
txtmasv.Focus();
txtmasv.Visible = true;
txtdiachi.Visible = true;
txtgioitinh.Visible = true;
txthoten.Visible = true;
txtngaysinh.Visible = true;
txtmasv.Text = "";
txthoten.Text = "";
txtgioitinh.Text = "";
txtngaysinh.Text = "";
txtdiachi.Text = "";
btncapnhat.Enabled = false;
btnluu.Enabled = true;
btnthem.Enabled = false;
btnsua.Enabled = false;
}
private bool kiemtratontai()
{
bool kt = false;
string masv = txtmasv.Text;
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
cnn.Open();
SqlDataAdapter da_kiemtra = new SqlDataAdapter("Select * from SINHVIEN where MASV='" + masv + "'", cnn);
DataTable dt_kiemtra = new DataTable();
da_kiemtra.Fill(dt_kiemtra);
if (dt_kiemtra.Rows.Count > 0)
{
kt = true;
}
da_kiemtra.Dispose();
return kt;
}
protected void btnluu_Click(object sender, EventArgs e)
{
lblthongbao.Text = "";
btnthem.Enabled = true;
btnsua.Enabled = true;
if (kiemtratontai() == true)
{
//lblthongbao.Text = "Bạn đã nhập trùng!";
Response.Write("Bạn đã nhập trùng");
cnn.Close();
}
else
{
string s = "INSERT INTO SINHVIEN(MASV,TENSV,GIOITINH,NGAYSINH,DIACHI) VALUES('" + txtmasv.Text + "','" + txthoten.Text + "','" + txtgioitinh.Text + "','" + txtngaysinh.Text + "','" + txtdiachi.Text + "')";
SqlCommand cmd = new SqlCommand(s, cnn);
cmd.ExecuteNonQuery();
cnn.Close();
lblthongbao.Text = "Bạn đã thêm thành công!";
adddata();
}
}
protected void btncapnhat_Click(object sender, EventArgs e)
{
lblthongbao.Text = "";
txtmasv.Visible = true;
txtdiachi.Visible = true;
txtgioitinh.Visible = true;
txthoten.Visible = true;
txtngaysinh.Visible = true;
btncapnhat.Enabled = false;
btnluu.Enabled = true;
btnthem.Enabled = false;
btnsua.Enabled = true;
}
protected void btnsua_Click(object sender, EventArgs e)
{string s = "UPDATE SINHVIEN SET TENSV=@TENSV,GIOITINH=@GIOITINH,NGAYSINH=@NGAYSINH,DIACHI=@DIACHI WHERE MASV=@MASV";
SqlCommand cmd = new SqlCommand(s, cnn);
cmd.Parameters.AddWithValue("@MASV", txtmasv.Text);
cmd.Parameters.AddWithValue("@TENSV", txthoten.Text);
cmd.Parameters.AddWithValue("@GIOITINH", txtgioitinh.Text);
cmd.Parameters.AddWithValue("@NGAYSINH", txtngaysinh.Text);
cmd.Parameters.AddWithValue("@DIACHI", txtdiachi.Text);
int up = 0;
try
{
cnn.Open();
up = cmd.ExecuteNonQuery();
}
catch (Exception err)
{
lblthongbao.Text = err.Message;
}
finally
{
cnn.Close();
if (up > 0)
{
adddata();
lblthongbao.Text = "Cập nhật thành công !";
Label1.Text = up.ToString();
}
}
}
protected void btnxoa_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("DELETE FROM SINHVIEN WHERE MASV='" + lstsv.SelectedItem.Value + "'");
cmd.Connection = cnn;
cmd.Parameters.AddWithValue("@MASV", txtmasv.Text);
int xoa = 0;
try
{
cnn.Open();
xoa = cmd.ExecuteNonQuery();
Label1.Text+= xoa.ToString();
}
catch(Exception err)
{
lblthongbao.Text = err.Message;
}
finally
{
cnn.Close();
if (xoa > 0)
{
lblthongbao.Text = "Bạn đã xóa thành công!";
txtmasv.Text = "";
txthoten.Text = "";
txtdiachi.Text = "";
txtgioitinh.Text = "";
txtngaysinh.Text = "";
}
}
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top