简体   繁体   中英

c# error on saving data from datagridview to Mysql

I have a problem on saving an items in the datagridview together with their orderno on the mysqldatabase, I've tried to use this codes:

 connection = new MySqlConnection(ConnectionString);
        connection.Open();
        command = new MySqlCommand(sql, connection);
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {

            sql = "insert into Item values('" + torderno.Text + "','" + dataGridView1.Rows[i].Cells["itemno"].Value + "');";
            command = new MySqlCommand(sql, connection);
            command.ExecuteNonQuery();
            //Column count doesn't match value count at row 1

        }
        connection.Close();

after I run it their is an error appeared which is this one: "Column count doesn't match value count at row 1"

what should I do? do you have an idea whats the problem of this?

Take a look at your table and see how many columns are there, in you insert query the number of values must equal the columns or you should write columns name (except auto incremental column) such as following:

insert into tblName(colName1,colName2,...) values (val1,val2,...);

or

insert into Item VALUES (vlueForCol1,valueForCol2,valueForCol3,...);

in other words your table has more columns than you are inserting into.

take a look at here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM