Exception









How do I use Exception
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


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: 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: 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         }
File name: frmThiThat.cs Copy
82         void TaoBangRandomCauTraLoi()
83         {
84             try
85             {
86                 SoCauHoi = BangDeThi.Rows.Count;
87                 string DapAnDung = "";
88                 string A, B, C, D;
89                 int DapAn;
90                 Random Rnd = new Random();
91                 ArrayList ArrDapAn = new ArrayList();
92                 for (int i = 0; i < SoCauHoi; i++)
93                 {
94                     A = "";
95                     B = "";
96                     C = "";
97                     D = "";
98                     DapAnDung = "";
99                     DapAn = 0;
100                     ArrDapAn.Clear();
101                     ArrDapAn.Add(2);
102                     ArrDapAn.Add(3);
103                     ArrDapAn.Add(4);
104                     ArrDapAn.Add(5);
105
106                     DapAn = Rnd.Next(ArrDapAn.Count);
107                     A = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
108                     if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
109                         DapAnDung += "A";
110                     ArrDapAn.RemoveAt(DapAn);
111                     DapAn = Rnd.Next(ArrDapAn.Count);
112                     B = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
113                     if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
114                         DapAnDung += "B";
115                     ArrDapAn.RemoveAt(DapAn);
116                     DapAn = Rnd.Next(ArrDapAn.Count);
117                     C = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
118                     if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
119                         DapAnDung += "C";
120                     ArrDapAn.RemoveAt(DapAn);
121                     DapAn = Rnd.Next(ArrDapAn.Count);
122                     D = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
123                     if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
124                         DapAnDung += "D";
125                     BangDeThi.Rows[i][2] = A;
126                     BangDeThi.Rows[i][3] = B;
127                     BangDeThi.Rows[i][4] = C;
128                     BangDeThi.Rows[i][5] = D;
129                     BangDeThi.Rows[i][6] = DapAnDung;
130                 }
131             }
132             catch (Exception ex)
133             { MessageBox.Show(ex.Message); }
134         }
File name: frmThiThat.cs Copy
135         void LoadCauHoiVaoControl()
136         {
137
138             try
139             {
140                 groupBox1.Text = "Câu: " + (CauHienTai + 1).ToString();
141                 lbCauHoi.Text = BangDeThi.Rows[CauHienTai][1].ToString();
142                 lbA.Text = BangDeThi.Rows[CauHienTai][2].ToString();
143                 lbB.Text = BangDeThi.Rows[CauHienTai][3].ToString();
144                 lbC.Text = BangDeThi.Rows[CauHienTai][4].ToString();
145                 lbD.Text = BangDeThi.Rows[CauHienTai][5].ToString();
146
147                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("A"))
148                     checkBox1.Checked = true;
149                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("B"))
150                     checkBox2.Checked = true;
151                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("C"))
152                     checkBox3.Checked = true;
153                 if (BangDeThi.Rows[CauHienTai][7].ToString().Contains("D"))
154                     checkBox4.Checked = true;
155             }
156             catch (Exception ex)
157             { MessageBox.Show(ex.Message); }
158         }
File name: frmThiThat.cs Copy
190         private void frmThiThat_Load(object sender, EventArgs e)
191         {
192
193
194             lbNgaythang.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
195
196             {
197                 try
198                 {
199                     if (DeThi == true)
200                         Load_DeThiThat();
201                     else
202                         Load_DeThiThat();
203                     TaoBangRandomCauTraLoi();
204                     LoadCauHoiVaoControl();
205                     timer1.Start();
206
207                     btPre.Enabled = false;
208                 }
209                 catch (Exception ex)
210                 { MessageBox.Show(ex.Message); }
211             }
212
213         }
File name: frmThiThat.cs Copy
215         private void btPre_Click(object sender, EventArgs e)
216         {
217             try
218             {
219                 GhiLaiDapAnTS();
220                 if (CauHienTai > 0)
221                 {
222                     GhiLaiDapAnTS();
223                     btNext.Enabled = true;
224                     checkBox1.Checked = false;
225                     checkBox2.Checked = false;
226                     checkBox3.Checked = false;
227                     checkBox4.Checked = false;
228                     CauHienTai--;
229                     LoadCauHoiVaoControl();
230                 }
231                 if (CauHienTai == 0)
232                     btPre.Enabled = false;
233             }
234             catch (Exception ex)
235             { MessageBox.Show(ex.Message); }
236         }
File name: frmThiThat.cs Copy
238         private void btNext_Click(object sender, EventArgs e)
239         {
240             try
241             {
242                 GhiLaiDapAnTS();
243                 if (CauHienTai < SoCauHoi - 1)
244                 {
245                     GhiLaiDapAnTS();
246                     btPre.Enabled = true;
247                     checkBox1.Checked = false;
248                     checkBox2.Checked = false;
249                     checkBox3.Checked = false;
250                     checkBox4.Checked = false;
251                     CauHienTai++;
252                     LoadCauHoiVaoControl();
253                 }
254                 if (CauHienTai == SoCauHoi - 1)
255                     btNext.Enabled = false;
256             }
257             catch (Exception ex)
258             { MessageBox.Show(ex.Message); }
259         }

Download file with original file name:Exception

Exception 131 lượt xem

Gõ tìm kiếm nhanh...