Exact MySQL statement works in Workbench but not in C# code

0

I'm trying to insert data into a table called 'stats_player' and trying to use the values I got from my input fields at the registration window. After I got the error

MySql.Data.MySqlClient.MySqlException (0x80004005): Column count doesn't match value count at row 1

So when I tried to run the exact insert statement into MySql Workbench the operation was successfully completed. But when I tried to copy and paste the operation command into my sqlstring in c# I got the same error.

Here is my sql operation:

    `INSERT INTO stats_player (date_updated,attack,ball_control,dribbling,low_pass,lofted_pass,finishing,
curve,header,defensive_skill,ball_winning,kick_power,speed,explosion,body_strenght,
physical_contact,jump,goalkeeping,catching,clearing,coverage,reflexes,stamina,
non_domminant_foot_usage,non_domminant_foot_precision,condition, injury_resistence,stats_club_id,stats_player_name)
VALUES ('2018-11-24',50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,1,'Messi');`

But when I put into my code it gets like this:

 private void metroTile2_Click(object sender, EventArgs e)
    {
        string sqlQuery = "INSERT INTO `stats_player` (date_updated,attack,ball_control," +
            "dribbling,low_pass,lofted_pass,finishing,curve,header,defensive_skill," +
            "ball_winning,kick_power,speed,explosion,body_strenght,physical_contact,jump," +
            "goalkeeping,catching,clearing,coverage,reflexes,stamina,non_domminant_foot_usage," +
            "non_domminant_foot_precision,`condition`,injury_resistence," +
            "stats_club_id,stats_player_name) " +
            "VALUES ('2018-11-24',50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50," +
            "50,50,50,50,50,1,'Messi');";

        /* These were the values that I will try to insert instead of those (50,50,50,50,...) values
         mtTrackBarAttack.Value           + "," +
         mtTrackBarBallControl.Value      + "," +
         mtTrackBarDribbling.Value        + "," +
         mtLowPassTB.Value                + "," +
         mtLoftedPassTB.Value             + "," +
         mtFinishingTB.Value              + "," +
         mtCurveTB.Value                  + "," +
         mtHeadingTB.Value                + "," +
         mtDefensiveTB.Value              + "," +
         mtBallWinningTB.Value            + "," +
         mtKickPowerTB.Value              + "," +
         mtSpeedTB.Value                  + "," +
         mtExplosionTB.Value              + "," +
         mtBodyStrenghtTB.Value           + "," +
         mtPhysicalContactTB.Value        + "," +
         mtJumpTB.Value                   + "," +
         mtGoalkeepingTB.Value            + "," +
         mtCatchingTB.Value               + "," +
         mtClearingTB.Value               + "," +
         mtCoverageTB.Value               + "," +
         mtReflexesTB.Value               + "," +
         mtStaminaTB.Value                + "," +
         mtNDFUsageTB.Value               + "," +
         mtNDFPrecisionTB.Value           + "," +
         mtConditionTB.Value              + "," +
         mtInjuryResistenceTB.Value       + "," +
         "1,'Messi'" + ");";  */

        MySqlConnection connection = new MySqlConnection(connectionString);
        MySqlCommand command = new MySqlCommand(sqlQuery, connection);
        command.CommandType = CommandType.Text;
        connection.Open();
        try
        {

            int i = command.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("Player successfully registered!");
            }
        }catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());

        }
        finally
        {
            connection.Close();
        }
    }

And I still get the same error, even with the exact same command! Does someone knows what is the problem here?

c#
mysql
database
visual-studio
asked on Stack Overflow Dec 12, 2018 by Mateus Arruda • edited Dec 12, 2018 by Steve

1 Answer

0

For some reason the the command worked after I restart Visual Studio. The commented code including the input values also worked. So if someone is trying for a long time and having this same error I suggest close and reopen the your Project Solution, it may work.

answered on Stack Overflow Dec 12, 2018 by Mateus Arruda

User contributions licensed under CC BY-SA 3.0