here table is creating but the values is not inserting

private void button_Click(object sender, EventArgs e)
        {
      SqlConnection cn;
      cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=J:\\Rcar\\motion\\db_image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
      if (cn.State==ConnectionState.Closed)
      {
        cn.Open();
      }
      SqlCommand cmd;
      string sq1;
      sq1 = "CREATE TABLE [" +Box1.Text+ "] (name CHAR(50) PRIMARY KEY," +"id CHAR(15), password CHAR(50), sex CHAR(10), email CHAR(15))"; 
      cmd = new SqlCommand(sq1, cn);
 
      try
      {
        cmd.ExecuteNonQuery();
        MessageBox.Show("Table Created");
      }
      catch (SqlException sqlexception)
      {
        MessageBox.Show(sqlexception.Message, "Oh Fudge.",MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message, "Fooey.", MessageBoxButtons.OK,MessageBoxIcon.Error);
      }
      finally
      {
       cn.Close();
      }
      try
      {
          cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=J:\\Rcar\\motion\\db_image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
          if (cn.State == ConnectionState.Closed)
          {
              cn.Open();
          }
          sq1 = "insert into [" + Box1.Text + "] values('" + accid.Text + "','" + pass.Text + "','" + sex.Text + "','" + email.Text + "')";
          cmd = new SqlCommand(sq1, cn);
          //cmd.ExecuteReader();
          MessageBox.Show("User Saved", "User craetion", MessageBoxButtons.OK, MessageBoxIcon.Information);
          cn.Close();
      }
      catch (Exception) { }
    }

Use have to to sqlExecuteNonQuery() method to exectute an insertion.

sq1 = "insert into [" + Box1.Text + "] values('" + accid.Text + "', '" + pass.Text + "', '" + sex.Text + "', '" + email.Text + "')"; 
cmd = new SqlCommand(sq1, cn);
cmd.ExecuteNonQuery(); //HERE!

still not inserting the values.............
the table is creating but the values is not inserting

what's the problem can anyone help me...............

Edited 5 Years Ago by Jessurider: n/a

Create a try catch block arund cmd.ExecuteNonQuery(); method. This way you will see what is the problem:
DO:

try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
}
This article has been dead for over six months. Start a new discussion instead.