thormayer
thormayer

Reputation: 1070

Refuses to make ExecuteNonQuery : Incorrect syntax near '('

Im trying to insert data to my database, and it gives me an error :

Incorrect syntax near '('.

This is my code :

string username = Session["Session"].ToString();

            con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
            con.Open();

            string knowWhichOne = "SELECT ID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            SqlCommand comm = new SqlCommand(knowWhichOne, con);
            int userID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT ClassID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int classID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT SchoolID FROM Users WHERE Username='"+UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int schoolID = (Int32)comm.ExecuteScalar();

            if (RadioWords.Checked == true)
            {
                 game = 1;
            }
            else
            {
                 game = 2;
            }


            string sqlqueryString = "INSERT INTO (GameID, UserID, LengthOfArray, NumberOfErrors, ClassID, SchoolID) VALUES (@GameID, @UserID, @LengthOfArray, @NumberOfErrors, @ClassID, @SchoolID)";

            SqlCommand commandquery = new SqlCommand(sqlqueryString, con);

            commandquery.Parameters.AddWithValue("GameID", game);
            commandquery.Parameters.AddWithValue("UserID", userID);
            commandquery.Parameters.AddWithValue("LengthOfArray", HowMany.Text);
            commandquery.Parameters.AddWithValue("NumberOfErrors", 0);
            commandquery.Parameters.AddWithValue("ClassID", classID);
            commandquery.Parameters.AddWithValue("SchoolID", schoolID);

            commandquery.ExecuteNonQuery();
            con.Close();

I run it in debug mode, and its accepting everything until the "ExecuteNonQuery();" line.

anybody has a clue what I did wrong?

Thanks!

Upvotes: 1

Views: 2901

Answers (3)

Purplegoldfish
Purplegoldfish

Reputation: 5294

The syntax of your insert into statement is incorrect as you are not specifying which table you are inserting into.

The correct syntax is

INSERT INTO table_name (column1, column2, column3,...)
 VALUES (value1, value2, value3,...)

See : http://www.w3schools.com/sql/sql_insert.asp for more information

Upvotes: 2

Mike Nakis
Mike Nakis

Reputation: 62110

Your INSERT INTO statement is missing the name of the table into which it is supposed to insert.

Upvotes: 3

juergen d
juergen d

Reputation: 204894

you did this:

INSERT INTO (GameID....

but should do this:

INSERT INTO tablename (GameID....

Upvotes: 8

Related Questions