the "?locatie" must be changed to "?Locatie".
System.Collections.Generic.KeyNotFoundException in mysql, mariadb
Rinaldo
396
Reputation points
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#
11,567 questions
Accepted answer
1 additional answer
Sort by: Most helpful
-
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