[英]Value cannot be null.Parameter name: key when databind in ASP.NET
I am triing to bind the data to a listbox from sql server then got the error "Value cannot be null.Parameter name: key" 我试图将数据从sql server绑定到列表框,然后收到错误“值不能为空。参数名称:键”
ddlCountry = new Obout.ComboBox.ComboBox();
ddlCountry.Width = 200;
ddlCountry.Height = 200;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
sqlCommand = "SELECT [Country] FROM [tbl_LookupCountry] where [Country] IS NOT NULL";
SqlConnection sqlConCountry = new SqlConnection(connectString);
SqlCommand sqlCommCountry = new SqlCommand();
sqlCommCountry.Connection = sqlConCountry;
sqlCommCountry.CommandType = System.Data.CommandType.Text;
sqlCommCountry.CommandText = sqlCommand;
sqlCommCountry.CommandTimeout = 300;
sqlConCountry.Open();
reader = sqlCommCountry.ExecuteReader();
ddlCountry.DataSource = reader;
ddlCountry.DataBind();
sqlConCountry.Close();
Does anyone meet this problem before? 有人遇到过这个问题吗?
Ideally you should return the columns similar to "Id" and the "Name" of the country. 理想情况下,您应该返回类似于国家/地区的“ Id”和“ Name”的列。 The Id would be the value for the dropdown and the Name would be the text which the user would see in the dropdown.
Id是下拉菜单的值,而Name是下拉菜单中用户看到的文本。
Make sure you have the DataTextField and DataValueField of the dropdownlist assigned the "Id" and "Name" columns respectively for teh above example. 确保在上例中为下拉列表的DataTextField和DataValueField分别分配了“ Id”和“ Name”列。
EDIT :- Pretty odd though, If this still does not work, could you try setting the SelectedValue of the dropdown to null just before the databind? 编辑 :-虽然很奇怪,但是如果仍然不行,您可以尝试在数据绑定之前将下拉列表的SelectedValue设置为null吗?
The other option to try to move the databinding code out of the If(!IsPostBack). 另一个尝试将数据绑定代码移出If(!IsPostBack)的选项。 This is odd again.
这又是奇怪的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.