title: how to fix "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll"
body: <p>when I run the code it runs. the login and register form appears. But after entering details when clicking on the register button it shows the error </p>
  
  <p>"An unhandled exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll"
  and highlight the <strong>con</strong> and <strong>con.open()</strong></p>
  
  <p>the coding is given below</p>
  
  <p>namespace MovieBookingSystemCSharp
  {
      public partial class Register : Form
      {
          public Register()
          {
              InitializeComponent();
          }</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          SqlConnection **con** = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\dell\Documents\Visual Studio 2015\Projects\MovieBookingSystemCSharp\MovieBookingSystemCSharp\movie.mdf;Integrated Security=True");
          **con.Open();**
  
          try
          {
              string str = "INSERT INTO user1(name,mobile,email,pass) VALUES('" + textBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "'); ";
  
              SqlCommand cmd = new SqlCommand(str, con);
              cmd.ExecuteNonQuery();
  
              //-------------------------------------------//
  
              string str1 = "select max(Id) from user1;";
  
              SqlCommand cmd1 = new SqlCommand(str1, con);
              SqlDataReader dr = cmd1.ExecuteReader();
              if (dr.Read())
              {
                  MessageBox.Show("New User Registered Successfully..");
                  Form1 obj = new Form1();
                  obj.ShowDialog();
                  this.Hide();
              }
          }
          catch (SqlException excep)
          {
              MessageBox.Show(excep.Message);
          }
          con.Close();
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          textBox1.Text = "";
          TextBox2.Text = "";
          TextBox3.Text = "";
          TextBox4.Text = "";
  
      }
  }
  </code></pre>
  
  <p>}</p>