我在填充字典並將數據放入列表框後出現了一些問題。 這裏是我的代碼:InvalidCastException和C#中的詞典
Dictionary<int, int> items = new Dictionary<int, int>();
MySqlCommand cmd = new MySqlCommand(query1, c.Connection);
rdr = cmd.ExecuteReader();
while (rdr.Read()) {
//InvalidCastException HERE
items.Add((int)rdr["artikel_id"], (int)rdr["sum(aantal)"]);
}
for (int i = 0; i < items.Count(); i++) {
if (items.ElementAt(i).Value <= 0) {
listBoxStrings.Add("artikel_id: " + items.ElementAt(i).Key + ", besteld: " + items.ElementAt(i).Value);
}
}
foreach (string s in listBoxStrings) {
//listbox
lb.Items.Add(s);
}
'artikel_id'列的類型是什麼? –
都是整數,在我的分貝 – ernie379
這看起來很奇怪'(INT)RDR [ 「總和(aantal)」]',你的意思'(INT)RDR [ 「aantal」]'? – oleksii