Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void btLog_Click(object sender, EventArgs e)
         {
             try
             {
                 if (txtdangnhap.Text == "" || txtmatkhau.Text == "")
                 {
                     MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                     txtdangnhap.Focus();
                     return;
                 }
                     if(txtdangnhap.Text=="admin" && txtmatkhau.Text=="admin")
                     {
                         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)
                         {
                             this.Hide();
                             frmQuanLy_Admin admin = new frmQuanLy_Admin();
                             admin.ShowDialog();

                         }

                     }
                 else
                 {
                     SqlConnection con = new SqlConnection("server=.\\SQLEXPRESS;database=QL_Thitracnghiem;integrated security=true");
                     con.Open();
                     SqlCommand cmd = new SqlCommand("Select MatKhau from THISINH where TenDangNhap ='" + txtdangnhap.Text + "'", con);
                     SqlDataReader reader = cmd.ExecuteReader();
                     reader.Read();
                     if (txtmatkhau.Text == reader.GetValue(0).ToString())
                     {
                         MessageBox.Show("Bạn đã đăng nhập thành công!", "Thành công");
                         reader.Close();
                         con.Close();
                         this.Close();
                         frmLuaChon frmChon = new frmLuaChon();
                         frmChon.ShowDialog();
                         this.Hide();

                     }
                     else
                     {
                         MessageBox.Show("Sai Mật Khẩu");
                         txtmatkhau.Focus();
                     }
                     reader.Close();
                     con.Close();
                 }

             }

             catch (Exception)
             {

                 MessageBox.Show("Tên đăng nhập và mật khẩu không hợp lệ","Có lỗi xảy ra");
                 txtdangnhap.Focus();

             }
         }