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) { }
    }

Recommended Answers

All 8 Replies

anyone please help me.............

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...............

Please use

cmd.ExecuteNonQuery();

hey that works............

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);
}

HEY IF I WANT TO INSERT NUMBERS ALONG WITH THE CHARACTERS WHAT DATA TYPE SHOULD I GIVEN.........eg email:nidhish31588@gmail.com

string (in database this would equal "varchar" - set it to 50 characthers lenght -thats enough

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.