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();
}
===============================================================================