首页 > 解决方案 > 填充数据适配器时字典中不存在给定的键

问题描述

字典中不存在给定的键。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Collections.Generic.KeyNotFoundException:字典中不存在给定的键。

MySqlConnection con = new MySqlConnection("Data Source=localhost; Database=gransells; User ID=root; Password=PASSWORD;");
MySqlCommand cmd = new MySqlCommand("select description from product where productId=1", con);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

DataSet ds = new DataSet();
adapter.Fill(ds);

标签: c#asp.netdatasetdataadapter

解决方案


您在正在设置但未检索的表中有主键。在 select 语句中使用该主键。


推荐阅读