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 btnSave_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 ct = "select ProductName from Product where ProductName='" + txtProductName.Text + "'";

                 cmd = new SqlCommand(ct);
                 cmd.Connection = con;
                 rdr = cmd.ExecuteReader();

                 if (rdr.Read())
                 {
                     MessageBox.Show("Product Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     txtProductName.Text = "";
                     txtProductName.Focus();


                     if ((rdr != null))
                     {
                         rdr.Close();
                     }
                     return;
                 }
                 auto();
                 con = new SqlConnection(cs.DBConn);
                 con.Open();
                 string cb = "insert into Product(ProductID,ProductName,CategoryID,SubCategoryID,Features,Price,Image) VALUES ('" + txtProductID.Text + "','" + txtProductName.Text + "'," + txtCategoryID.Text+ "," + txtSubCategoryID.Text+ ",@d1,"+ txtPrice.Text +",@d2)";
                 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 saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 Autocomplete();
                 btnSave.Enabled = false;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }