Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have one window form with one textbox1 and button1, Here I m adding new column to the Table Employee, and The column name should be the textbox.text name.
private void button1_Click(object sender, EventArgs e)
{
 string strConn = @"Password=Admin123;Persist Security Info=True;User ID=sa;Initial Catalog=Employee;Data Source=NAVUY-24B";
 SqlConnection conn = new SqlConnection(strConn);
 conn.Open();
SqlCommand sqlCmd = new SqlCommand("alter table Employee ADD '" + textBox1.Text + "' varchar(50)", conn);
sqlCmd.ExecuteNonQuery();
}


its not working
Incorrect syntax near at textbox......

pls any one help me...
thanks in advance
Posted

Obviously your code is terrible. I expect this is just an experiment and that you'll learn to write good code later. In general, what you're doing is a bad idea, too, because why would you write code that adds columns at random ?

What you should do, is grab the text you're generating, paste it in to SQL Server Management tools, and see what error message you get when you try to run it. Then you can work out how to fix your SQL. I don't believe that a column name goes in quotes, however ( assuming the rest of your SQL is correct, which I am not sure of )
 
Share this answer
 
Comments
RakeshMeena 14-Jun-11 4:30am    
My 5!
SqlCommand sqlCmd = new SqlCommand("alter table Employee ADD " + textBox1.Text + "; varchar(50)", conn);
remove the single quot of the text box
 
Share this answer
 
Comments
Christian Graus 14-Jun-11 4:00am    
Thanks for explaining that, my response was obviously not clear.....
sindhu s 14-Jun-11 4:32am    
thanku... its working fine....

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