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:

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

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

Recommended Answers

All 2 Replies

You are missing the keyword COLUMN:

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

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

Page.Response.Redirect("myform.aspx")

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 :)

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.