简体   繁体   中英

Editing datagridview. Visual C# 2008

I have the following code:

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        string cellid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); //to działa, ale nie przenosi na %s 

        MySqlConnection mysqlCon = new MySqlConnection("SERVER = naven.com.pl; DATABASE = naven1_terminarz; User ID = naven1_terminarz; PASSWORD = mydbpassword;");
        MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` = 'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);

        //MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` = 'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);

            cmd.Parameters.AddWithValue("@ID", MySqlDbType.Int32).Value = Int32.Parse(cellid);
            cmd.Parameters.AddWithValue("@VALUE", MySqlDbType.VarChar).Value = value;


            try
            {
                mysqlCon.Open();
                cmd.ExecuteNonQuery();
                mysqlCon.Close();
                //MessageBox.Show(cmd.ToString());
                //MessageBox.Show("@ID @VALUE"); //this one displays @ID @VALUE instead of variable value.
                MessageBox.Show(cellid); //this one is working fine, but i don't know how to input this value into MySql query


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

    }

The MySql code itself is working, however @ID and @VALUE are not displaying correctly, it simply says @ID instead of integer and @VALUE instead of my string value. I was rewriting this code: naven.com.pl/cellendedit.jpg It looks the same(or does it?) and it was working for the author, and for me it's not.

As you can read in code MessageBox.Show(cellid); Is working but It doesn't work with @ID @VALUE variables.

I checked the query by inputting correct values instead of variables and it was working with this code.

What could be the reason?

Problem : You have enclosed your named parameters within single quotes.

Solution : You need to remove the single quotes around the following 2 parameters

@VALUE
@ID

Replace This:

    MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
    `Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` = 
      'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 
                 'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);

With This:

    MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
    `Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = @VALUE,`Tekst` = 
      'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 
                 'dsa' WHERE `Terminarz`.`id` = @ID LIMIT 1 ;", mysqlCon);

Try

MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
    `Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = @VALUE,`Tekst` = 
      'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 
                 'dsa' WHERE `Terminarz`.`id` = @ID LIMIT 1 ;", mysqlCon);

correction :removed quotes '' from @VALUE and @ID

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