1

I am receiving the following error and cannot find the problem:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''String', 'String', '6', 'String', 'String', 'String', 'The' at line 1

C# code:

    protected void updateDB(byte[] content, int size, string mime)
{
    string school = DropDownList_School.Text;
    string subject = DropDownList_Subject.Text;
    string teacher = DropDownList_Teacher.Text;
    string course = DropDownList_Class.Text;
    string unit = TextBox_Unit.Text;
    string name = TextBox_Name.Text;
    string date = DateTime.Today.ToString();
    string username = System.Web.Security.Membership.GetUser().UserName;
    MySqlConnection connection = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);
    connection.Open();
    MySqlCommand cmd = new MySqlCommand();//(query, connection);
    cmd.Connection = connection;
    cmd.CommandText = "INSERT INTO uploads 
                       (@School, @Subject, @Teacher, @Course, @Unit, @Name, @Username, @Size, @MIME, @content) ";
    cmd.Prepare();
    cmd.Parameters.AddWithValue("@School", school);
    cmd.Parameters.AddWithValue("@Subject", subject);
    cmd.Parameters.AddWithValue("@Teacher", teacher);
    cmd.Parameters.AddWithValue("@Course", course);
    cmd.Parameters.AddWithValue("@Unit", unit);
    cmd.Parameters.AddWithValue("@Name", name);
    cmd.Parameters.AddWithValue("@Username", username);
    cmd.Parameters.AddWithValue("@Size", size);
    cmd.Parameters.AddWithValue("@MIME", mime);
    cmd.Parameters.AddWithValue("@content", content);
    cmd.ExecuteNonQuery();
    connection.Close();
}

The ConnectionString is correct.

3
  • try INSERT INTO upload (field1, field2) VALUES (@value1, @value2) Jul 14, 2011 at 2:44
  • @Fernando Costa : the fields & VALUES isn't necessary if inserting values into all fields in the table -- the OPs query is valid
    – OMG Ponies
    Jul 14, 2011 at 2:55
  • 1
    @OMG Ponies, good practice to use them because if they are of the same type then sql could guess which column incorrectly (especially if you put them in the wrong order accidentally and the types are valid) Jul 14, 2011 at 2:56

1 Answer 1

1

should be INSERT INTO uploads VALUES(...) You might also want to specify the column order such as INSERT INTO uploads(col1, col2, etc...) VALUES(val for col1, val for col2..., etc) because if not then SQL will try to "guess" and it can guess wrong.

5
  • Thanks! yeah I have to go with the second one. Converting to prepared statements and missed a step.
    – Cameron S
    Jul 14, 2011 at 2:45
  • was my answer, is that wrong?!? try INSERT INTO upload (field1, field2) VALUES (@value1, @value2) ... 0_o Jul 14, 2011 at 3:15
  • its columns not fields, also I answered it first Jul 14, 2011 at 3:15
  • well fernando answered first i think... but it wasnt an answer... it was a comment. no way to accept that right?
    – Cameron S
    Jul 14, 2011 at 4:51
  • nope and if you check the times I did answer first, I edited to fix a formatting error i had with a code tag Jul 14, 2011 at 4:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.