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 Button7_Click(object sender, EventArgs e)
         {
             try
             {
                 if (txtCustomerID.Text == "")
                 {
                     MessageBox.Show("Please retrieve Customer ID", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     txtCustomerID.Focus();
                     return;
                 }

                 if (txtProductName.Text=="")
                 {
                     MessageBox.Show("Please retrieve product name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return;
                 }
                 if (txtSaleQty.Text=="")
                 {
                     MessageBox.Show("Please enter no. of buy quantity", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     txtSaleQty.Focus();
                     return;
                 }
                 int SaleQty = Convert.ToInt32(txtSaleQty.Text);
                 if (SaleQty == 0)
                 {
                     MessageBox.Show("no. of sale quantity can not be zero", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     txtSaleQty.Focus();
                     return;
                 }

                 if (ListView1.Items.Count==0)
                 {

                     ListViewItem lst = new ListViewItem();
                     lst.SubItems.Add(txtProductID.Text);
                     lst.SubItems.Add(txtProductName.Text);
                     lst.SubItems.Add(txtPrice.Text);
                     lst.SubItems.Add(txtSaleQty.Text);
                     lst.SubItems.Add(txtTotalAmount.Text);
                     ListView1.Items.Add(lst);
                     txtSubTotal.Text = subtot().ToString();

                     Calculate();
                     txtProductName.Text = "";
                     txtProductID.Text = "";
                     txtPrice.Text = "";
                     txtAvailableQty.Text = "";
                     txtSaleQty.Text = "";
                     txtTotalAmount.Text = "";
                     txtProduct.Text = "";
                     return;
                 }

                 for (int j = 0; j <= ListView1.Items.Count - 1; j++)
                 {
                     if (ListView1.Items[j].SubItems[1].Text == txtProductID.Text)
                     {
                         ListView1.Items[j].SubItems[1].Text = txtProductID.Text;
                         ListView1.Items[j].SubItems[2].Text = txtProductName.Text;
                         ListView1.Items[j].SubItems[3].Text = txtPrice.Text;
                         ListView1.Items[j].SubItems[4].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[4].Text) + Convert.ToInt32(txtSaleQty.Text)).ToString();
                         ListView1.Items[j].SubItems[5].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[5].Text) + Convert.ToInt32(txtTotalAmount.Text)).ToString();
                         txtSubTotal.Text = subtot().ToString();
                         Calculate();
                         txtProductName.Text = "";
                         txtProductID.Text = "";
                         txtPrice.Text = "";
                         txtAvailableQty.Text = "";
                         txtSaleQty.Text = "";
                         txtTotalAmount.Text = "";
                         return;

                     }
                 }

                     ListViewItem lst1 = new ListViewItem();

                     lst1.SubItems.Add(txtProductID.Text);
                     lst1.SubItems.Add(txtProductName.Text);
                     lst1.SubItems.Add(txtPrice.Text);
                     lst1.SubItems.Add(txtSaleQty.Text);
                     lst1.SubItems.Add(txtTotalAmount.Text);
                     ListView1.Items.Add(lst1);
                     txtSubTotal.Text = subtot().ToString();
                     Calculate();
                     txtProductName.Text = "";
                     txtProductID.Text = "";
                     txtPrice.Text = "";
                     txtAvailableQty.Text = "";
                     txtSaleQty.Text = "";
                     txtTotalAmount.Text = "";
                     return;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }