Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

C#
private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               SqlConnection con = new SqlConnection(@"Data Source=ABHINAV-PC\SQLEXPRESS;Initial Catalog=E1;Integrated Security=True;");
               con.Open();
               string str = "CREATE TABLE'"+textBox1.Text+"(userid number(10),password varchar2(20),email varchar2(20));'";
               SqlCommand cmd = new SqlCommand(str, con);
               cmd.ExecuteNonQuery();
               MessageBox.Show("Table created");
           }
           catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
       }


What I have tried:

i dont know what i'm doing wrong in this create table query...the textbox field will decide the table name
Posted
Updated 18-Mar-16 18:57pm
Comments
King Fisher 19-Mar-16 1:36am    
Just copy the str value while debugging and try to run in SQL server,then match with create table syntax.

1 solution

There is a space missing between Table and TableName. Secondly query is not properly formatted(single quote):
C#
SqlConnection con = new SqlConnection(@"Data Source=ABHINAV-PC\SQLEXPRESS;Initial Catalog=E1;Integrated Security=True;");
con.Open();
string str = "CREATE TABLE "+ textBox1.Text +" (userid number(10), password varchar2(20), email varchar2(20));";

SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
 
Share this answer
 
v2
Comments
Member 10549697 19-Mar-16 5:58am    
Thank You so much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900