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 DataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
         {
             try
             {
                 DataGridViewRow dr = DataGridView1.SelectedRows[0];
                 this.Hide();
                 frmSales frmSales = new frmSales();
                 // or simply use column name instead of index
                 // dr.Cells["id"].Value.ToString();
                 frmSales.Show();
                 frmSales.txtInvoiceNo.Text = dr.Cells[0].Value.ToString();
                 frmSales.dtpInvoiceDate.Text = dr.Cells[1].Value.ToString();
                 frmSales.txtCustomerID.Text = dr.Cells[2].Value.ToString();
                 frmSales.txtCustomerName.Text = dr.Cells[3].Value.ToString();
                 frmSales.txtSubTotal.Text = dr.Cells[4].Value.ToString();
                 frmSales.txtTaxPer.Text = dr.Cells[5].Value.ToString();
                 frmSales.txtTaxAmt.Text = dr.Cells[6].Value.ToString();
                 frmSales.txtDiscountPer.Text = dr.Cells[7].Value.ToString();
                 frmSales.txtDiscountAmount.Text = dr.Cells[8].Value.ToString();
                 frmSales.txtTotal.Text = dr.Cells[9].Value.ToString();
                 frmSales.txtTotalPayment.Text = dr.Cells[10].Value.ToString();
                 frmSales.txtPaymentDue.Text = dr.Cells[11].Value.ToString();
                 frmSales.cmbPaymentType .Text = dr.Cells[12].Value.ToString();
                 frmSales.cmbStatus.Text = dr.Cells[13].Value.ToString();
                 frmSales.txtRemarks.Text = dr.Cells[14].Value.ToString();
                 frmSales.btnUpdate.Enabled = true;
                 frmSales.Delete.Enabled = true;
                 frmSales.btnPrint.Enabled = true;
                 frmSales.Save.Enabled = false;
                 frmSales.lblUser.Text = lblUser.Text;
                 frmSales.lblUserType.Text = lblUserType.Text;
                 con = new SqlConnection(cs.DBConn);
                 con.Open();
                 cmd = new SqlCommand("SELECT Product.ProductID,ProductSold.Productname,ProductSold.Price,ProductSold.Quantity,ProductSold.TotalAmount from Invoice_Info,ProductSold,Product where Invoice_info.InvoiceNo=ProductSold.InvoiceNo and Product.ProductID=ProductSold.ProductID and invoice_Info.InvoiceNo='" + dr.Cells[0].Value.ToString() + "'", con);
                 rdr = cmd.ExecuteReader();
                 while (rdr.Read()==true)
                 {
                     ListViewItem lst = new ListViewItem();
                     lst.SubItems.Add(rdr[0].ToString().Trim());
                     lst.SubItems.Add(rdr[1].ToString().Trim());
                     lst.SubItems.Add(rdr[2].ToString().Trim());
                     lst.SubItems.Add(rdr[3].ToString().Trim());
                     lst.SubItems.Add(rdr[4].ToString().Trim());
                     frmSales.ListView1.Items.Add(lst);
                  }
                 frmSales.ListView1.Enabled = false;
                 frmSales.Button7.Enabled = false;
              }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }