System.Collections.Generic.KeyNotFoundException in mysql, mariadb

Rinaldo 396 Reputation points
2023-12-25T10:32:20.26+00:00

Hi all,

Got a problem with an execute command in SQL with mariadb For writing to the database i've made a custum routine which worked, but after an update of the server i run into a unresolved error. The error is as following:

System.Collections.Generic.KeyNotFoundException
i

 server.Execute(
     "INSERT INTO Voorraad (Aantal, Artikel, Inkoop, Verkoop, Locatie, Catagorie, Barcode, " +
     "Leverancier, DatumAankoop, onSite, isRetour, minimum)" +
     " VALUES (?Aantal, ?Artikel, ?Inkoop, ?Verkoop, ?Locatie, ?Catagorie," +
     " ?Barcode, ?Leverancier, ?DatumAankoop, ?onSite, ?isretour, ?Minimum)",
     "?Aantal", int.Parse(Aantal),
     "?Artikel", Onderdeel,
     "?Inkoop", decimal.Parse(Prijs),
     "?Verkoop", decimal.Parse(verkoopPrijs),
     "?locatie", Locatie,
     "?Catagorie", catagorie,
     "?Barcode", barCode,
     "?Leverancier", leverancier,
     "?DatumAankoop", DateTime.Now,
     "?onSite", onSite,
     "?isretour", false,
     "?Minimum", 0
 );
            public MySqlCommand Execute(string command, params object[] objectList)         {             int trying = 0;             string volg = "";              try             {                  if (isAlive)                 {                      cmd.Connection = mySqlConnection;                     cmd.CommandText = command;                      if (objectList == null || objectList.Length == 0)                     {                         if (command.Length > 0)                         {                             cmd.Prepare();                             cmd.ExecuteNonQuery();                         }                         return cmd;                     }                       //cmd.ExecuteNonQuery();                     int Teller = 0;                      object[] isObjects = new object[objectList.Length];                      foreach (object o in objectList)                     {                         isObjects[Teller++] = o;                     }                      //cmd.Prepare();                      for (int j = 0; j < Teller; j += 2)                     {                         Type t = isObjects[j + 1].GetType();                         trying = j;                         volg = isObjects[trying].ToString();                          if (t.Equals(typeof(int)))                         {                             cmd.Parameters.AddWithValue(isObjects[j].ToString(), (int)isObjects[j + 1]);                             continue;                         }                          if (t.Equals(typeof(string)))                         {                             cmd.Parameters.AddWithValue(isObjects[j].ToString(), isObjects[j + 1]);                             continue;                         }                          if (t.Equals(typeof(decimal)))                         {                             cmd.Parameters.AddWithValue(isObjects[j].ToString(), (decimal)isObjects[j + 1]);                             continue;                         }                          if (t.Equals(typeof(long)))                         {                             cmd.Parameters.AddWithValue(isObjects[j].ToString(), (long)isObjects[j + 1]);                             continue;                         }                          if (t.Equals(typeof(bool)))                         {                             cmd.Parameters.AddWithValue(isObjects[j].ToString(), MySqlDbType.Bit).Value = (bool)isObjects[j + 1];                              continue;                         }                          if (t.Equals(typeof(DateTime)))                         {                             cmd.Parameters.Add(isObjects[j].ToString(), MySqlDbType.Date).Value = (DateTime)isObjects[j + 1];                              continue;                         }                     }                        }                      cmd.ExecuteNonQuery(); 
Developer technologies C#
{count} votes

Accepted answer
  1. Morad Aziz 80 Reputation points
    2023-12-25T14:08:20.2433333+00:00

    the "?locatie" must be changed to "?Locatie".

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rinaldo 396 Reputation points
    2023-12-25T14:27:13.92+00:00

    Thanks for the answers, the change of the letters did help but the main thing was the MySql.Data.DLL I had removed the dll from the referenced and placed a newer version of the dll in the refences and the problem was disappeared. Thanks all, it did cost me a night of sleep


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.