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 button1_Click(object sender, EventArgs e)
         {
             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\GymManagementSystem in C#\GymManagementSystem\Gym.mdf;Integrated Security=True;User Instance=True");
             con.Open();
             string gender = string.Empty;
             if (radioButton1.Checked)
             {
                 gender = "Male";
             }
             else if (radioButton2.Checked)
             {
                 gender = "Female";
             }
             try
             {
                 string str = " INSERT INTO staff(f_name,l_name,gender,dob,mobile,email,jod,street,city) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + gender + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" +textBox8.Text + "'); ";

                 SqlCommand cmd = new SqlCommand(str, con);
                 cmd.ExecuteNonQuery();

                 string str1 = "select max(stf_id) from staff;";

                 SqlCommand cmd1 = new SqlCommand(str1, con);
                 SqlDataReader dr = cmd1.ExecuteReader();
                 if (dr.Read())
                 {
                     MessageBox.Show("" + textBox1.Text + "'s Details is Inserted Successfully.. " + textBox1.Text + "'s Id is " + dr.GetInt32(0) + ".", "Important Message");
                     this.Hide();

                 }
                 this.Close();
             }
             catch (SqlException excep)
             {
                 MessageBox.Show(excep.Message);
             }
             con.Close();
         }