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 textBox1_TextChanged(object sender, EventArgs e)
         {
             textBox2.Text = "";
             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Sem.4\C# Projects\College management system\CollegeManagementSystem\SchoolManagementSystem\school.mdf;Integrated Security=True;User Instance=True");

             con.Open();
             if (textBox1.Text != "")
             {
                 try
                 {
                     string getCust = "select name,standard,medium from student where std_id=" + Convert.ToInt32(textBox1.Text) + " ;"; // saving new custmer info

                     SqlCommand cmd = new SqlCommand(getCust, con);
                     SqlDataReader dr;
                     dr = cmd.ExecuteReader();
                     if (dr.Read())
                     {
                         label9.Text = dr.GetValue(0).ToString();
                         label6.Text = dr.GetValue(1).ToString();
                         label7.Text = dr.GetValue(2).ToString();
                     }
                     else
                     {
                         MessageBox.Show("Sorry '" + textBox1.Text + "' This Registration Id is Invalid, Please Insert Correct Id");
                         textBox1.Text = "";
                         textBox2.Text = "";
                     }
                 }
                 catch (SqlException excep)
                 {
                     MessageBox.Show(excep.Message);
                 }
                 con.Close();
             }
         }