hello to all. I have a problem. I want to alter a table to add three columns whenever i click a button.But i want the name of the table taken from a drop down list, and the name of the three columns are also taken from either a drop down list or a textbox. Here's the code that i used as trial for the first column:

        cmd = New SqlCommand("ALTER TABLE " + ddl_sub.Text + " ADD " + txt_desc.Text + " varchar(50) ", con)
        cmd.CommandType = CommandType.Text

However, when i try it, it say "incorrect syntax near varchar" and when i add (') the error becomes "incorrect syntax near 'quiz'". If i change it back, the code works but if i click the button again, "incorrect syntax near varchar" appears again... Help please.. Is my code wrong? i'm new at this. By the way im making use of sql server

6 Years
Discussion Span
Last Post by Eniekko

You are missing the keyword COLUMN:

cmd = New SqlCommand("ALTER TABLE " + ddl_sub.Text + " ADD COLUMN " + txt_desc.Text + " varchar(50) ", con)
Votes + Comments
Yep :)

well, thanks for the reply. but when i tried adding the word COLUMN, it still showed an error..

There's this part from my code


when i tried removing it, my code as it is, actually worked. I don't know if the redirect code has got anything to do with it but now its working.

Thanks a lot though :)

Edited by Eniekko: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.