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

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.

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

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

Edited 5 Years Ago by Jessurider: n/a

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.

Edited 5 Years Ago by Momerath: n/a

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

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

Edited 5 Years Ago by Jessurider: n/a

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)";
This question has already been answered. Start a new discussion instead.