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
             {
                 if (txtProductname.Text == "")
                 {
                     MessageBox.Show("Please enter product name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     txtProductname.Focus();
                     return;
                 }
                 Cursor = Cursors.WaitCursor;
                 timer1.Enabled = true;
                 rptStock rpt = new rptStock();
                 //The report you created.
                 cmd = new SqlCommand();
                 SqlDataAdapter myDA = new SqlDataAdapter();
                 POS_DBDataSet myDS = new POS_DBDataSet();
                 //The DataSet you created.
                 con = new SqlConnection(cs.DBConn);
                 cmd.Connection = con;
                 cmd.CommandText = "SELECT * from Stock,Product,Supplier where Stock.ProductID=Product.ProductID and Stock.SupplierID=Supplier.SupplierID and ProductName like '" + txtProductname.Text + "%' order by StockDate";
                 cmd.CommandType = CommandType.Text;
                 myDA.SelectCommand = cmd;
                 myDA.Fill(myDS, "Stock");
                 myDA.Fill(myDS, "Product");
                 myDA.Fill(myDS, "Supplier");
                 rpt.SetDataSource(myDS);
                 frmStockReport frm = new frmStockReport();
                 frm.crystalReportViewer1.ReportSource = rpt;
                 frm.Visible = true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }