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 [B]"+TextBox1.Text+"[/B](myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)"; 
       
      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();
      }
        }

IN THE ABOVE CODE CAN CREATE THE TextBox1.Text VALUE AS THE NEW TABLE........

CAN ANYONE TELL ME WHAT'S THE PROBLEM

Recommended Answers

All 9 Replies

YOu forgot to include ' ' to the query around property textbox.Text). Your create query has to look like:

sq1 = "CREATE TABLE '" + TextBox1.Text + "'(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

I don't see the constraint clause in your table creation. You'll also need to change the name of the constraint for each table.

Now an error message came as

invalid syntax

still error in this code..........

sq1 = "CREATE TABLE '" +TextBox1.Text+ "'(myId INTEGER PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

You don't need the tick marks (') since that isn't part of the create table syntax. You might need [] around the table name depending on what is in the textbox.

how?
like this............

sq1 = "CREATE TABLE " +[TextBox1.Text]+ "(myId INTEGER PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";
sq1 = "CREATE TABLE [" +TextBox1.Text+ "} (myId INTEGER PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

hey this works pretty nice...............

sq1 = "CREATE TABLE [" +TextBox1.Text+ "] (myId INTEGER PRIMARY KEY," +"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

thanks.................

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.