Mokey
Mokey

Reputation: 215

C# Mysql Parameters Issue

I'm trying to write a query to my database. When I originally wrote it I didn't use parameters, I am now trying to do so But I am getting a Mysql Error now.

Column count doesn't match value at row1

Which in my 1st column I have an auto Incremented number labeled as "ID" Which I do not believe I have to add to the query because of the auto increment..

This is the newly written code:

            MySqlConnection conn = new MySqlConnection(dbConnection);
            string Query = "INSERT INTO tasklist.tasks (TaskName, AssignedTo, StartDate, EndDate, Description, DirPath, Completed) VALUES (?TaskName),(?AssignedTo),(?StartDate),(?EndDate),(?Description),(?DirPath),(?Completed);";

            MySqlCommand insertCommand = new MySqlCommand(Query, conn);

            conn.Open();

            insertCommand.Parameters.AddWithValue("?TaskName", txtTaskName.Text);
            insertCommand.Parameters.AddWithValue("?AssignedTo", txtAssignTo.Text);
            insertCommand.Parameters.AddWithValue("?StartDate", txtdateStart.Value.ToString("yyyy-M-d"));
            insertCommand.Parameters.AddWithValue("?EndDate", txtDateEnd.Value.ToString("yyyy-M-d"));
            insertCommand.Parameters.AddWithValue("?Description", txtTaskDescription.Text);
            insertCommand.Parameters.AddWithValue("?DirPath", txtDirPath.Text);
            insertCommand.Parameters.AddWithValue("?Completed",("0"));

            insertCommand.ExecuteNonQuery();

            conn.Close();

Remember the previous way without parameters worked... however with parameters does not work. What exactly am I doing wrong here? Any help would be greatly appreciated.

Upvotes: 1

Views: 68

Answers (1)

juergen d
juergen d

Reputation: 204746

Use only one pair of parentheses around all parameters

INSERT INTO tasklist.tasks (TaskName, AssignedTo, StartDate, EndDate, Description, DirPath, Completed) 
VALUES (?TaskName, ?AssignedTo, ?StartDate, ?EndDate, ?Description, ?DirPath, ?Completed)

Upvotes: 3

Related Questions