Try
How do I use Try
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
87 | try | 1 |
79 | try | 2 |
185 | try | 3 |
319 | try | 4 |
32 | try | 5 |
68 | try | 6 |
69 | try | 7 |
99 | try | 8 |
36 | try | 9 |
53 | try | 10 |
File name: FormXemDiem.cs
Copy
85 private void btnXuatRaExcel_Click(object sender, EventArgs e)
86 {
87 try
88 {
89
90 Excel.Application objExcelApp = new Excel.Application();
91 Excel.Workbook objExcelWorkbook = objExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
92 Excel.Worksheet objSheet = (Excel.Worksheet)objExcelWorkbook.Worksheets[1];
93 //objSheet.Cells.Interior.Color.ToString() = 'blue';
94 objSheet = (Excel.Worksheet)objExcelWorkbook.Sheets[1];
95 objSheet.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
96 objSheet.Cells[2, 5] = "BẢNG ĐIỂM CỦA SINH VIÊN";
97 objSheet.Cells[5, 3] = "Mã Học Phần";
98 objSheet.Cells[5, 4] = "Mã Nhóm Học Phần";
99 objSheet.Cells[5, 5] = "Tên Học Phần";
100 objSheet.Cells[5, 6] = "Điểm Thi";
101 objSheet.Cells[5, 7] = "Điểm Quá Trình";
102 objSheet.Cells[5, 8] = "Điểm Xếp Loại";
103 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
104 objSheet.Cells[4, 3] = "Mã SV : " + txtMaSV.Text + "";
105 objSheet.Cells[9,10] = "ĐIỂM TỔNG KẾT";
106 objSheet.Cells[10,10] = "Hệ 10 : "+txtHe10.Text+"";
107 objSheet.Cells[11,10] = "Hệ 4 : "+txtHe4.Text+"";
108 for (int i = 0; i < dgvKetQua.Rows.Count ; i++)
109 {
110 for (int j = 0; j < dgvKetQua.Columns.GetColumnCount(DataGridViewElementStates.Displayed); j++)
111 {
112 objSheet.Cells[i + 6, j + 3] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
113 objSheet.Columns.AutoFit();
114 }
115 }
116 objExcelApp.Visible = true;
117 }
118 catch (Exception ex)
119 {
120 MessageBox.Show(ex.Message);
121 }
122 }
File name: FormXemDiemLop.cs
Copy
77 private void btnXuatRaExcel_Click(object sender, EventArgs e)
78 {
79 try
80 {
81
82 Excel.Application objExcelApp = new Excel.Application();
83 Excel.Workbook objExcelWorkbook = objExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
84 Excel.Worksheet objSheet = (Excel.Worksheet)objExcelWorkbook.Worksheets[1];
85 //objSheet.Cells.Interior.Color.ToString() = 'blue';
86 objSheet = (Excel.Worksheet)objExcelWorkbook.Sheets[1];
87 objSheet.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
88 objSheet.Cells[2, 5] = "BẢNG ĐIỂM CỦA LỚP";
89 objSheet.Cells[3, 3] = "Mã Học Kỳ : "+cmbMaHK.SelectedValue.ToString()+"";
90 objSheet.Cells[4, 3] = "Mã Lớp : " + cmbMaLop.SelectedValue.ToString() + "";
91 objSheet.Cells[5, 4] = "Mã Sinh Viên";
92 objSheet.Cells[5, 5] = "Tên Sinh Viên";
93 objSheet.Cells[5, 6] = "Điểm Trung Bình";
94 //select sv.MaSV,TenSV,TrungBinhCuoiKyHe10
95
96 for (int i = 0; i < dgvKetQua.Rows.Count ; i++)
97 {
98 for (int j = 0; j < dgvKetQua.Columns.GetColumnCount(DataGridViewElementStates.Displayed); j++)
99 {
100 objSheet.Cells[i + 6, j + 4] = dgvKetQua.Rows[i].Cells[j].Value.ToString();
101 objSheet.Columns.AutoFit();
102 }
103 }
104 objExcelApp.Visible = true;
105 }
106 catch (Exception ex)
107 {
108 MessageBox.Show(ex.Message);
109 }
110 }
File name: frmCapNhat.cs
Copy
183 private void button1_Click_1(object sender, EventArgs e)
184 {
185 try
186 {
187 string sCauA = textBox1.Text;
188 string sCauB = textBox2.Text;
189 string sCauC = textBox3.Text;
190 string sCauD = textBox4.Text;
191 string sMaCH = textBox5.Text;
192 string sCauHoi = richTextBox1.Text;
193 string sCauDung = "";
194 string sCauDungLuu = "";
195 string sTrinhDo = comboBox1.Text;
196 if (richTextBox1.Text.ToString().Trim() == "" || textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || (checkBox1.Checked == false && checkBox2.Checked == false && checkBox3.Checked == false && checkBox4.Checked == false))
197 {
198 MessageBox.Show("Bạn đã sai làm thao tác", "Thông báo");
199 }
200 else
201 {
202 if (checkBox1.Checked == true)
203 {
204 sCauDung = "A";
205 sCauDungLuu += sCauDung;
206 }
207 if (checkBox2.Checked == true)
208 {
209 sCauDung = "B";
210 sCauDungLuu += sCauDung;
211 }
212 if (checkBox3.Checked == true)
213 {
214 sCauDung = "C";
215 sCauDungLuu += sCauDung;
216 }
217 if (checkBox4.Checked == true)
218 {
219 sCauDung = "D";
220 sCauDungLuu += sCauDung;
221 }
222
223 sqlcmd = new SqlCommand("update dapan set A='" + sCauA + "',B='" + sCauB + "',C='" + sCauC + "',D='" + sCauD + "',dapan='" + sCauDungLuu + "' where mach='" + sMaCH + "'", sqlcon);
224 sqlcmd.ExecuteNonQuery();
225 sqlcmd = new SqlCommand("update cauhoi set cauhoi='" + sCauHoi + "', trinhdo='" + sTrinhDo + "' where mach='" + sMaCH + "'", sqlcon);
226 sqlcmd.ExecuteNonQuery();
227 LoadDataToListView();
228 Bandau();
229 }
230 }
231
232 catch
233 {
234 MessageBox.Show("Bạn đã sai làm thao tác", "Thông báo");
235 }
236 }
File name: frmCapNhat.cs
Copy
311 private void button4_Click(object sender, EventArgs e)
312 {
313 if (richTextBox1.Text == "" || textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || (checkBox1.Checked == false && checkBox2.Checked == false && checkBox3.Checked == false && checkBox4.Checked == false))
314 {
315 MessageBox.Show("Bạn chưa thêm đầy đủ thông tin", "Thông báo");
316 }
317 else
318 {
319 try
320 {
321
322 string sCauA = textBox1.Text;
323 string sCauB = textBox2.Text;
324 string sCauC = textBox3.Text;
325 string sCauD = textBox4.Text;
326 string sMaCH = textBox5.Text;
327 string sCauHoi = richTextBox1.Text;
328 string sCauDung = "";
329 string sCauDungLuu = "";
330 string sTrinhDo = comboBox1.Text;
331 if (checkBox1.Checked == true)
332 {
333 sCauDung = "A";
334 sCauDungLuu += sCauDung;
335 }
336 if (checkBox2.Checked == true)
337 {
338 sCauDung = "B";
339 sCauDungLuu += sCauDung;
340 }
341 if (checkBox3.Checked == true)
342 {
343 sCauDung = "C";
344 sCauDungLuu += sCauDung;
345 }
346 if (checkBox4.Checked == true)
347 {
348 sCauDung = "D";
349 sCauDungLuu += sCauDung;
350 }
351
352 sqlcmd = new SqlCommand("insert into dapan values('" + sMaCH + "','" + sCauA + "','" + sCauB + "','" + sCauC + "','" + sCauD + "','" + sCauDungLuu + "') ", sqlcon);
353 sqlcmd.ExecuteNonQuery();
354 sqlcmd = new SqlCommand("insert into cauhoi values('" + sMaCH + "','" + sCauHoi + "','" + sTrinhDo + "') ", sqlcon);
355 sqlcmd.ExecuteNonQuery();
356 LoadDataToListView();
357 button4.Enabled = false;
358 }
359 //roi hen;de bi coi lai thu de cHOAky thu chay thu hen ok
360 catch
361 {
362 MessageBox.Show("Bạn đã thao tác sai");
363 }
364 }
365 }
File name: frmDienTenTS.cs
Copy
30 private void btbatdauthi_Click(object sender, EventArgs e)
31 {
32 try
33 {
34 if (tbten.Text == "" || tbmathisinh.Text == "")
35 {
36 MessageBox.Show("Bạn phải điền đầy đủ thông tin");
37 }
38 else
39 {
40 string TenThSinh = tbten.Text;
41 string MaThiSinh = tbmathisinh.Text;
42 string strSQL = "insert THISINHTHI values('" + MaThiSinh + "','" + TenThSinh + "')";
43 SqlCommand ObjCmd = new SqlCommand(strSQL, ObjCon);
44 ObjCmd.ExecuteNonQuery();
45 if (MessageBox.Show("Bạn có chắc chắn thi không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
46 {
47
48 string dienten = this.tbten.Text;//chuyen thong tin sang form thithat
49 string dienma = this.tbmathisinh.Text;
50 frmThiThat thithat = new frmThiThat(dienten, dienma);
51 thithat.ShowDialog();
52 this.Close();
53
54
55 }
56
57 }
58 }
59 catch
60 {
61 MessageBox.Show("Mã thí sinh trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
62 }
63
64 }
File name: frmLogin.cs
Copy
66 private void btLog_Click(object sender, EventArgs e)
67 {
68 try
69 {
70 if (txtdangnhap.Text == "" || txtmatkhau.Text == "")
71 {
72 MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
73 txtdangnhap.Focus();
74 return;
75 }
76 if(txtdangnhap.Text=="admin" && txtmatkhau.Text=="admin")
77 {
78 if (MessageBox.Show("Chào mừng bạn đã đến với mục quản lý dành cho giáo viên", "Thông báo", MessageBoxButtons.OK) == DialogResult.OK)
79 {
80 this.Hide();
81 frmQuanLy_Admin admin = new frmQuanLy_Admin();
82 admin.ShowDialog();
83
84 }
85
86 }
87 else
88 {
89 SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS;database=QL_Thitracnghiem;integrated security=true");
90 con.Open();
91 SqlCommand cmd = new SqlCommand("Select MatKhau from THISINH where TenDangNhap ='" + txtdangnhap.Text + "'", con);
92 SqlDataReader reader = cmd.ExecuteReader();
93 reader.Read();
94 if (txtmatkhau.Text == reader.GetValue(0).ToString())
95 {
96 MessageBox.Show("Bạn đã đăng nhập thành công!", "Thành công");
97 reader.Close();
98 con.Close();
99 this.Close();
100 frmLuaChon frmChon = new frmLuaChon();
101 frmChon.ShowDialog();
102 this.Hide();
103
104 }
105 else
106 {
107 MessageBox.Show("Sai Mật Khẩu");
108 txtmatkhau.Focus();
109 }
110 reader.Close();
111 con.Close();
112 }
113
114 }
115
116 catch (Exception)
117 {
118
119 MessageBox.Show("Tên đăng nhập và mật khẩu không hợp lệ","Có lỗi xảy ra");
120 txtdangnhap.Focus();
121
122 }
123 }
File name: frmQuanLySinhVien.cs
Copy
67 private void btnthem_Click(object sender, EventArgs e)
68 {
69 try
70 {
71 string mats = textBox1.Text;
72 string hoten = textBox2.Text;
73 string ngaysinh = textBox3.Text;
74 string gioitinh = textBox4.Text;
75 string quequan = textBox5.Text;
76 string sodt = textBox6.Text;
77 string email = textBox7.Text;
78
79 string strSQL = "insert SINHVIEN values('" + mats + "','" + hoten + "','" + ngaysinh + "','" + gioitinh + "','" + quequan + "','" + sodt + "','" + email + "')";
80 sqlcmd = new SqlCommand(strSQL, sqlcon);
81 sqlcmd.ExecuteNonQuery();
82 MessageBox.Show("B?n dã dang ký thành công");
83 load();
84 }
85 catch
86 {
87 MessageBox.Show("Trùng mã sinh viên !!!");
88 }
89
90 }
File name: frmQuanLySinhVien.cs
Copy
97 private void btnsua_Click(object sender, EventArgs e)
98 {
99 try
100 {
101 string mats = textBox1.Text;
102 string hoten = textBox2.Text;
103 string ngaysinh = textBox3.Text;
104 string gioitinh = textBox4.Text;
105 string quequan = textBox5.Text;
106 string sodt = textBox6.Text;
107 string email = textBox7.Text;
108 sqlcmd = new SqlCommand("update sinhvien set mats='" + mats + "',hoten='" + hoten + "',ngaysinh='" + ngaysinh + "',gioitinh='" + gioitinh + "',quequan='" + quequan + "',sodienthoai='" + sodt + "',email'" + email + "' ", sqlcon);
109 sqlcmd.ExecuteNonQuery();
110
111 load();
112 }
113 catch
114 {
115 MessageBox.Show("Loi");
116 }
117 }
File name: frmThiThat.cs
Copy
34 void Load_DeThiThat()
35 {
36 try
37 {
38 SqlConnection cnn = new SqlConnection("server=.\\SQLEXPRESS;database=QL_Thitracnghiem;integrated security =true");
39 SqlDataAdapter da = new SqlDataAdapter("select cauhoi.mach,cauhoi,a,b,c,d ,dapan from cauhoi,DAPAN where cauhoi.mach=DAPAN.mach", cnn);
40 DataTable BangQuestion = new DataTable();
41 da.Fill(BangQuestion);
42 TaoBangRandomCauHoi(BangQuestion);
43 }
44 catch (Exception ex)
45 { MessageBox.Show(ex.Message); }
46 p = 60;//nhap thoi gian thi
47 s = 60;
48 h = 0;
49 }
File name: frmThiThat.cs
Copy
50 void TaoBangRandomCauHoi(DataTable BangQuestion)
51 {
52
53 try
54 {
55 Random Rnd = new Random();
56 ArrayList ArrQuestion = new ArrayList();
57
58 ArrQuestion.Clear();
59 int x, dem = 0;
60 int SoCauTrongBangGoc = BangQuestion.Rows.Count;
61 while (dem < SoCauNgauNhien)
62 {
63 x = Rnd.Next(0, SoCauTrongBangGoc);
64 if (!ArrQuestion.Contains(x))
65 {
66 ArrQuestion.Add(x);
67 dem++;
68 }
69 }
70 for (int j = SoCauTrongBangGoc - 1; j >= 0; j--)
71 if (!ArrQuestion.Contains(j))
72 BangQuestion.Rows.RemoveAt(j);
73
74 BangDeThi = BangQuestion;
75 BangDeThi.Columns.Add("cauhoi,DAPAN");
76
77 }
78 catch (Exception ex)
79 { MessageBox.Show(ex.Message); }
80
81 }
Download file with original file name:Try
Try 110 lượt xem
Gõ tìm kiếm nhanh...