0

Hello I got a big problem I am trying to add a new column to my MSSQL Database Table and i tried it like thousand times but it wont work. My destination is to press a button then use the function "eventsspalte_Hinzufügen" to add a new column with the name thats Inserted by the user.

This is the snippet.

private void eventsspalte_Hinzufügen()
{

    SQL_eingabe = "ALTER TABLE Teilnahmen_Events ADD @tbName bit NOT NULL ;"; //  CONSTRAINT strconst3 DEFAULT 0

    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = SQL_eingabe;
    cmd.Parameters.AddWithValue("@tbName", tb_Eventname.Text);
    cmd.ExecuteNonQuery();
    con.Close();
}

The Exception says that cmd.ExecuteQuery() is not able to Execute the sql Command because of the wromg Syntax at @tbName I also tried to use a variable like:

ALTER TABLE Teilnahmen_Events ADD'"+ tb_Eventname.Text +"'bit NOT NULL ;";

but it also didnt work... I hope you got an solution for me thank you very much.

1
  • 1
    When it didnt work - what was the error? Commented Mar 29, 2017 at 9:07

1 Answer 1

2

you cannot pass column name as parameter.

In your second example, single quotes are not needed, so change it into

ALTER TABLE Teilnahmen_Events ADD "+ tb_Eventname.Text +" bit NOT NULL ;";
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much !! i just sit here for 2 days like the stupid retard I really am ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.