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 btnUpdate_Click(object sender, EventArgs e)
         {
             if (txtProductName.Text == "")
             {
                 MessageBox.Show("Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 txtProductName.Focus();
                 return;
             }
             if (cmbCategory.Text == "")
             {
                 MessageBox.Show("Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 cmbCategory.Focus();
                 return;
             }
             if (cmbSubCategory.Text == "")
             {
                 MessageBox.Show("Please select sub category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 cmbSubCategory.Focus();
                 return;
             }
             if (txtPrice.Text == "")
             {
                 MessageBox.Show("Please enter price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 txtPrice.Focus();
                 return;
             }
             try
             {

                 con = new SqlConnection(cs.DBConn);
                 con.Open();
                 string cb = "Update product set ProductName='" + txtProductName.Text + "',CategoryID=" + txtCategoryID.Text + ",SubCategoryID=" + txtSubCategoryID.Text + ",Features=@d1,price=" + txtPrice.Text + ",Image=@d2 Where ProductID='" + txtProductID.Text + "'";
                 cmd = new SqlCommand(cb);
                 cmd.Connection = con;
                 cmd.Parameters.AddWithValue("@d1", txtFeatures.Text);
                 MemoryStream ms = new MemoryStream();
                 Bitmap bmpImage = new Bitmap(pictureBox1.Image);
                 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] data = ms.GetBuffer();
                 SqlParameter p = new SqlParameter("@d2", SqlDbType.Image);
                 p.Value = data;
                 cmd.Parameters.Add(p);
                 cmd.ExecuteReader();
                 con.Close();
                 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 Autocomplete();
                 btnUpdate.Enabled = false;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }