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 Button5_Click(object sender, EventArgs e)
         {
             try
             {
                 GroupBox10.Visible = true;
                 con = new SqlConnection(cs.DBConn);
                 con.Open();
                 cmd = new SqlCommand("SELECT RTRIM(invoiceNo) as [Order No],RTRIM(InvoiceDate) as [Order Date],RTRIM(Invoice_Info.CustomerID) as [Customer ID],RTRIM(CustomerName) as [Customer Name],RTRIM(SubTotal) as [SubTotal],RTRIM(VATPer) as [Vat+ST %],RTRIM(VATAmount) as [VAT+ST Amount],RTRIM(DiscountPer) as [Discount %],RTRIM(DiscountAmount) as [Discount Amount],RTRIM(GrandTotal) as [Grand Total],RTRIM(TotalPayment) as [Total Payment],RTRIM(PaymentDue) as [Payment Due],RTRIM(PaymentType) as [Payment Type],RTRIM(Status) as [Status],Remarks from Invoice_Info,Customer where Invoice_Info.CustomerID=Customer.CustomerID and InvoiceDate between @d1 and @d2 and PaymentDue > 0 order by InvoiceDate desc", con);
                 cmd.Parameters.Add("@d1", SqlDbType.DateTime, 30, "InvoiceDate").Value = DateTimePicker2.Value.Date;
                 cmd.Parameters.Add("@d2", SqlDbType.DateTime, 30, "InvoiceDate").Value = DateTimePicker1.Value.Date;
                 SqlDataAdapter myDA = new SqlDataAdapter(cmd);
                 DataSet myDataSet = new DataSet();
                 myDA.Fill(myDataSet, "Invoice_Info");
                 myDA.Fill(myDataSet, "Customer");
                 DataGridView2.DataSource = myDataSet.Tables["Customer"].DefaultView;
                 DataGridView2.DataSource = myDataSet.Tables["Invoice_Info"].DefaultView;
                 Int64 sum = 0;
                 Int64 sum1 = 0;
                 Int64 sum2 = 0;

                 foreach (DataGridViewRow r in this.DataGridView2.Rows)
                 {
                     Int64 i = Convert.ToInt64(r.Cells[9].Value);
                     Int64 j = Convert.ToInt64(r.Cells[10].Value);
                     Int64 k = Convert.ToInt64(r.Cells[11].Value);
                     sum = sum + i;
                     sum1 = sum1 + j;
                     sum2 = sum2 + k;
                 }
                 TextBox12.Text = sum.ToString();
                 TextBox11.Text = sum1.ToString();
                 TextBox10.Text = sum2.ToString();

                 con.Close();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }