1

I am getting an exception from this code, saying that I have an error in my sql syntax. Furthermore, it says that this error is at line 1, which makes no sense, since line one has just says: using System; I have used more than an hour debugging the code, and I can't figure it out. Below is the code. Can anyone see a mistake? Thanks

public void SaveReferenceToDatabase(string billedNavn, string placering, string description, DateTime date)
    {
        Sql sql = new Sql();

        string query = "INSERT INTO GalleryImages (BilledNavn, Placering, Beskrivelse, Dato)" + 
                       "VALUES (?billedNavn, ?placering, ?description, ?date";

        MySqlCommand cmd = sql.Command(query);

        cmd.Parameters.AddWithValue("billedNavn", billedNavn);
        cmd.Parameters.AddWithValue("placering", placering);
        cmd.Parameters.AddWithValue("description", description);
        cmd.Parameters.AddWithValue("date", date);

        sql.ConnOpen();
        cmd.ExecuteNonQuery();

        sql.ConnClose();
    }

Ps. The exception is thrown when the cmd.ExecuteNonQuery() is beeing executed

6
  • 1
    Line 1 likely refers to line 1 of your MySQL statement. The only problem I can see is your string building. Have you thought of the space between "Dato)" and "VALUES"?
    – Dreami
    Aug 16, 2013 at 23:16
  • 1
    I dont remember nothing about c#, but it like as sql error, a quickly look like is missing the close parenthesis in the query Aug 16, 2013 at 23:16
  • @EmilioGort did you mean c# instead of c++? Aug 16, 2013 at 23:18
  • yes...i fixed...my mistake Aug 16, 2013 at 23:18
  • So would you mind marking my answer as the solution?
    – FrankPl
    Aug 16, 2013 at 23:31

1 Answer 1

7

You are missing the closing ) of the values() clause of the SQL code (second line of SQL code).

The error message states that the error is in line one of the SQL code, and that is as you have no line break in your SQL string that you build (which is completely fine, just may be a bit irritating when searching the error location).

2
  • 1
    This doesn't justify it's own answer, so I'll piggy back, but the other possible error is the fact there is no space between Dato) and VALUES. So it would look like Dato)VALUES if it was on one line, which would cause an error.
    – Jetti
    Aug 17, 2013 at 0:42
  • Thank you folks, it was the closing ) that caused it. I can't believe I couldn't see that. I guess I had been staring at the damn code for too long. Thanks again
    – AomSet
    Aug 17, 2013 at 10:13

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.