1

Im tring to save data on mysql db but i always get "Column count doesn't match value count at row 1"

I tried to recode this sql command, seen some videos about this but cant found nothing that work

string Insert = 
    "INSERT INTO db.pcs(Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('" 
            + nome + "," + marca + "," + user + "," + ram + "," 
            + cpu + "," + disco + "," + tiposo + "," + so + "," 
            + lice + "," + tipopc + "," + progs + "')";

using (MySqlConnection cn = new MySqlConnection(connst))
{
    cn.Open();
    MySqlCommand cmd = new MySqlCommand(Insert, cn);

    try
    {
        if (cmd.ExecuteNonQuery() == 1)
        {
            pb1.Visible = true;
            timer1.Start();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Its supose to add to db but on the try messagebox i get "Column count doesn't match value count at row 1"

SCouto
  • 7,808
  • 5
  • 32
  • 49
David
  • 26
  • 3
  • 2
    Your script is at risk for SQL Injection Attacks. – gunr2171 Jun 03 '19 at 14:29
  • I know but for the moment im just testing this for use on my home only me who gonna use this program – David Jun 03 '19 at 14:35
  • 2
    Possible duplicate of [Column count doesn't match value count at row 1](https://stackoverflow.com/questions/18369252/column-count-doesnt-match-value-count-at-row-1) – trailmax Jun 03 '19 at 14:38

2 Answers2

0

I think you should make " ' " for each value

string Insert = 
"INSERT INTO  db.pcs (Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('" 
        + nome + "','" + marca + "','" + user + "','" + ram + "','" 
        + cpu + "','" + disco + "','" + tiposo + "','" + so + "','" 
        + lice + "','" + tipopc + "','" + progs + "')";
Amine
  • 142
  • 5
0

You set only one quote at the start and one at the end. So it is only one value you set into your insert command.

Try this or use sqlparams:

            string Insert =
            "INSERT INTO db.pcs(Nome,Marca,Utilizador,Ram,CPU,Disco,TipoSO,SO,Licenca,TipoPC,ProgramaseLicenca) VALUES('"
                    + nome + "', '" + marca + "', '" + user + "', '" + ram + "', '"
                    + cpu + "', '" + disco + "', '" + tiposo + "', '" + so + "', '"
                    + lice + "', '" + tipopc + "', '" + progs + "')";