0% found this document useful (0 votes)
7 views3 pages

Câu 2

The document contains a C# code snippet for a Windows Forms application that interacts with a local API to manage student data. It includes methods for loading student information, displaying class lists in a ComboBox, searching for students, adding, editing, and deleting student records. The application uses HTTP requests to communicate with the API and updates the UI accordingly based on user actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Câu 2

The document contains a C# code snippet for a Windows Forms application that interacts with a local API to manage student data. It includes methods for loading student information, displaying class lists in a ComboBox, searching for students, adding, editing, and deleting student records. The application uses HTTP requests to communicate with the API and updates the UI accordingly based on user actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Câu 2

private void Form1_Load(object sender, EventArgs e)


{
Hienthi();
}
---------------------------------------------------------------------------
private void Hienthi()
{
string link = "http://localhost/data/api/default/listSV";
HttpWebRequest req = HttpWebRequest.CreateHttp(link);
WebResponse res = req.GetResponse();
DataContractJsonSerializer js = new
DataContractJsonSerializer(typeof(Sinhvien[]));
object data = js.ReadObject(res.GetResponseStream());
Sinhvien[] danhsach = (Sinhvien[])data;
dataGridView1.DataSource = danhsach;
Danhsachlop();
}
-----------------------------------------------------------------------------
private void Danhsachlop() /// ComboBox
{
string link = "http://localhost/data/api/default/dslop";
HttpWebRequest req = HttpWebRequest.CreateHttp(link);
WebResponse res = req.GetResponse();
DataContractJsonSerializer js = new
DataContractJsonSerializer(typeof(Lophoc[]));
object data = js.ReadObject(res.GetResponseStream());
Lophoc[] danhsach = (Lophoc[])data;
cboLophoc.DataSource = danhsach;
cboLophoc.ValueMember = "malop";
cboLophoc.DisplayMember = "tenlop";
}
-----------------------------------------------------------------------------
private void btnTim_Click(object sender, EventArgs e)
{
string str = string.Format("?masv={0}", txtMasv.Text);
string link = "http://localhost/data/api/default/tim"+str;
HttpWebRequest req = HttpWebRequest.CreateHttp(link);
WebResponse res = req.GetResponse();
DataContractJsonSerializer js = new
DataContractJsonSerializer(typeof(Sinhvien));
object data = js.ReadObject(res.GetResponseStream());
Sinhvien sv = (Sinhvien)data;
if (sv != null)
{
Lophoc l = new Lophoc();
txtHoten.Text = sv.hoten;
txtDiachi.Text = sv.diachi;
txtDienthoai.Text = sv.dienthoai;
cboLophoc.SelectedValue = sv.malop;
txtAnh.Text = sv.anh;
// txtMalop.Text = sv.malop.ToString();

}
else
{
MessageBox.Show("K tim thay sinh vien co ma:"+ txtMasv.Text);
}
}
-------------------------------------------------------------------------------

private void btnDanhsach_Click(object sender, EventArgs e)


{
Hienthi();
}
=============================================================================

private void btnThem_Click(object sender, EventArgs e)


{
string link = "http://localhost/data/api/default/themsv";
var client = new WebClient();
var sv = new NameValueCollection();
sv["hoten"] = txtHoten.Text;
sv["diachi"] = txtDiachi.Text;
sv["dienthoai"] = txtDienthoai.Text;
//sv["malop"] = txtMalop.Text;
sv["malop"] = cboLophoc.SelectedValue.ToString();
sv["anh"] = txtAnh.Text;
var res = client.UploadValues(link, sv);
string msg = Encoding.UTF8.GetString(res);
MessageBox.Show("Ket qua them" + msg);
Hienthi();
}
===============================================================================
private void btnSua_Click(object sender, EventArgs e)
{
string link = "http://localhost/data/api/default/sua";
var client = new WebClient();
var sv = new NameValueCollection();
sv["hoten"] = txtHoten.Text;
sv["diachi"] = txtDiachi.Text;
sv["dienthoai"] = txtDienthoai.Text;
//sv["malop"] = txtMalop.Text;
sv["malop"] = cboLophoc.SelectedValue.ToString();
sv["anh"] = txtAnh.Text;
sv["masv"] = txtMasv.Text;

var res = client.UploadValues(link,"PUT", sv);


string msg = Encoding.UTF8.GetString(res);
MessageBox.Show("Ket qua Sua" + msg);
Hienthi();
}
===============================================================================
private void btnXoa_Click(object sender, EventArgs e)
{
string str = string.Format("?masv={0}", txtMasv.Text);
string link = "http://localhost/data/api/xoa"+ str;
HttpWebRequest req = HttpWebRequest.CreateHttp(link);
req.Method = "DELETE";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
if(res.StatusCode == HttpStatusCode.OK)
{
MessageBox.Show("Da xoa sv co ma" + txtMasv.Text, "Thong bao");

}
else
{
MessageBox.Show("Co loi khi xoa" + txtMasv.Text,"Thong bao");
}
Hienthi();
}
===============================================================================

You might also like