简体   繁体   中英

How to create MySql Update Command to work with DataGridView?

I am trying to configure my DataAdapter UpdateCommand but it isn't working and I don't know why.

Error: Fatal error encountered during command execution

I am using a DataGridView "dgMaterials" which I changed the column HeaderText, I don't know if I have to use the HeaderText of the column or the original database field name in the command.

Below it's what I have, I am using just the field name as in the db.

MySqlCommand command = new MySqlCommand("UPDATE tblmaterial SET number_material = @number_material, qtdp_material = @qtdp_material, name_material = @name_material, material_material = @material_material, measurep_material = @measurep_material, date_material = @date_material, status_material = @status_material, qtdu_material = @qtdu_material, measureu_material = @measureu_material, provider_material = @provider_material, price_material = @price_material WHERE id_material = @id_material", pblCnx);
command.Parameters.Add(@"number_material", MySqlDbType.Int32, 1, "number_material");
command.Parameters.Add(@"qtdp_material", MySqlDbType.Int32, 3, "qtdp_material");
command.Parameters.Add(@"name_material", MySqlDbType.VarChar, 30, "name_material");
command.Parameters.Add(@"material_material", MySqlDbType.VarChar, 30, "material_material");
command.Parameters.Add(@"measurep_material", MySqlDbType.VarChar, 30, "measurep_material");
command.Parameters.Add(@"date_material", MySqlDbType.VarChar, 10, "date_material");
command.Parameters.Add(@"status_material", MySqlDbType.VarChar, 30, "status_material");
command.Parameters.Add(@"qtdu_material", MySqlDbType.Int32, 3, "qtdu_material");
command.Parameters.Add(@"provider_material", MySqlDbType.VarChar, 30, "provider_material");
command.Parameters.Add(@"price_material", MySqlDbType.Double, 6, "price_material");
MySqlParameter parameter = command.Parameters.Add(@"id_material", MySqlDbType.Int32, 4, "id_material");
parameter.SourceVersion = DataRowVersion.Original;
da.UpdateCommand = command;
dgMaterials.EndEdit();
da.Update(ds, "tblmaterial");

Please, help me!

add this event to datagridview "CellEndEdit"

and take this idea

private void dtg_contatos_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string query;
            string name;
            string cod;

            for (int i = 0; i < dtg_contatos.ColumnCount; i++)
            {
                if (i == 0)
                {
                    cod = dtg_contatos[i, e.RowIndex].Value.ToString();
                }
                if (i == 1)
                {
                    name = dtg_contatos[i, e.RowIndex].Value.ToString();
                }
            }
            query = "update table set name = '" + name + "' where cod = '" + cod + "';";
        }

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