I have been trying to add a column programmatically in ASP.NET to modify the tables in SQL Server.
Please see the following code:
string suppliernotxt = supplieridlist[1].ToString();
//SqlCommand cmd2 = new SqlCommand("ALTER TABLE [ProductNormalDB] ADD suppliernotxt nvarchar(20) NULL", con);
SqlCommand cmd2 = new SqlCommand("ALTER TABLE ProductNormalDB ADD @supplierlist nvarchar(20) NULL", con);
cmd2.Parameters.AddWithValue("@supplierlist", suppliernotxt);
//cmd2.Parameters.AddWithValue("@supplierlist", suppliernotxt.ToString());
//cmd2.Parameters["@supplierlist"].Value = supplieridlist[x];
cmd2.ExecuteNonQuery();
supplieridlist
is an array that acquires all the column names to add into the SQL Server database. For some reason the parametrized method is not working and shows the following error:
Incorrect syntax near '@supplierlist'.
The basic idea is to have a user select from a check box the name of the suppliers, based on the selected number of suppliers the array will create the supplier names for ex. if we selected 3 suppliers, the array will save "Supplier1"
, "Supplier2"
, "Supplier3"
and then the SqlCommand
is supposed to alter the table and add the new columns.