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 btnSave_Click(object sender, EventArgs e)
         {
             if (btnupdate.Enabled==false)
             {
                 try
                 {
                     tbThe test = (db.tbThes.Where(t => t.MaThe == txtMaThe.Text).SingleOrDefault());
                     if (test!=null)
                     {
                         MessageBox.Show("Mã Thẻ Đã Tòn Tại","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                         return;
                     }
                     else
                     {
                         if (db.CRUDThe('t', txtMaThe.Text, cbxMaDG.Text, mklNgayCap.Text, mktHan.Text) == 1)
                         {
                            db.SubmitChanges();
                            MessageBox.Show("Thêm Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         }
                         else
                             MessageBox.Show("Thêm Thất Bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     }

                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message, "Quản Lý Thư Viện");
                 }
                 finally
                 {
                     tbTHE.LoadGridView(dataGridViewX1);
                     btninsert.Image = Resources.add1_;
                     BtnThem(true);
                     txtMaThe.ReadOnly = true;
                     btnSave.Enabled = false;
                 }
             }
             if (btninsert.Enabled==false)
             {
                 try
                 {
                     if (MessageBox.Show("Bạn Có Muốn Sửa Không","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                     {
                         tbThe test = (db.tbThes.Where(t => t.MaThe == txtMaThe.Text).SingleOrDefault());
                         if (test != null)
                         {
                             if (db.CRUDThe('t',txtMaThe.Text,cbxMaDG.Text,mklNgayCap.Text,mktHan.Text)==1)
                             {
                                 db.SubmitChanges();
                                 MessageBox.Show("Sửa Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             }
                             else
                             {
                                 MessageBox.Show("Sửa Thất Bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                             }
                         }
                         else
                             MessageBox.Show("Mã Thẻ Không Tòn Tại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                     }

                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show(ex.Message, "Quản Lý Thư Viện");
                 }
                 finally
                 {

                     //var query = (from tbTHE in db.tbThes
                     // join tbDOCGIA in db.tbDGs
                     // on tbTHE.MaDG equals tbDOCGIA.MaDG
                     // select new
                     // {
                     // tbDOCGIA.MaDG,
                     // tbDOCGIA.TenDG,
                     // tbDOCGIA.ChucVu,
                     // tbTHE.MaThe,
                     // tbTHE.NgayCap,
                     // tbTHE.Han
                     // });
                     //dataGridViewX1.DataSource = query;
                     tbThe tbthenew = new tbThe();
                     tbthenew.LoadGridView(dataGridViewX1);
                     BtnThem(true);
                     txtMaThe.ReadOnly = true;
                     mklNgayCap.ReadOnly = true;
                     mktHan.ReadOnly = false;
                     btnupdate.Image = Resources.edit;
                     btnSave.Enabled = false;
                 }
             }
         }