title: C# Form not inserting values into SQL Server database
body: <p>I have a simple create a new user form which takes values from two text boxes, username and password. The button2 click event should take these values and insert them into the Users table in the database. However, when I run my code the message box appears to say the data has been added, I cannot see the data in the database using VS2010.</p>
  
  <p>See screen shot for the database connection in VS. I have also created a datasource of the database in VS.</p>
  
  <p>Any Ideas?</p>
  
  <p>Much appreciated.</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
      {
          string username = txtUsername.Text;
          string password = txtPassword.Text;
          string sqlquery;
          string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Nick\Documents\Visual Studio 2010\Projects\DebenhamsProjectOffice V.01\DebenhamsProjectOffice V.01\DebenhamsProjectOfficeDatabase.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
          SqlConnection cn = new SqlConnection(connection);
          try
          {
              cn.Open();
          }
          catch (Exception)
          {
              MessageBox.Show("Unable to connect to Database");
          }
  
          sqlquery = "INSERT INTO Users (Username, Password) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "')";
          try
          {
              SqlCommand command = new SqlCommand(sqlquery, cn);
              command.Parameters.AddWithValue("Username", username);
              command.Parameters.AddWithValue("Password", password);
              command.Parameters.Clear();
              MessageBox.Show("User Added");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          txtUsername.Text = "";
          txtPassword.Text = "";
          cn.Close();
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/arrK9.png" alt="enter image description here"></p>