简体   繁体   English

值不能为null参数名称:在ASP.NET中进行数据绑定时的键

[英]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.

相关问题 无法使用 ASP.NET Identity 登录,值不能为空。参数名称:manager - Can not sign in using ASP.NET Identity, Value cannot be null.Parameter name: manager 值不能为空。 参数名称:Asp.net中的源 - Value cannot be null. Parameter name: source in Asp.net 调整JPEG品质时出错:值不能为空参数名称:结构 - Error on adjusting jpeg quality: Value cannot be null.Parameter name: structure 值不能为null。参数名称:读取嵌入文件内容时出现流错误 - Value cannot be null.Parameter name: stream error while reading the contents of embeddded file “row”参数不能为空。参数名称:row - 'row' argument cannot be null.Parameter name: row ASP.NET Core 2.0 值不能为空。 参数名称:名称 - ASP.NET Core 2.0 Value cannot be null. Parameter name: Name 值不能是 null。 ASP.NET Core 3..0 中的(参数'key') - Value cannot be null. (Parameter 'key') in ASP.NET Core 3..0 Asp.Net Core 2.0 ArgumentNullException:值不能为null。 参数名称:connectionString - Asp.Net Core 2.0 ArgumentNullException: Value cannot be null. Parameter name: connectionString MVC3 asp.net错误:值不能为null。参数名称:下拉列表中的项目 - MVC3 asp.net error: Value cannot be null. Parameter name: items on dropdownlist ASP.NET MVC SelectList中的错误。 值不能为null,参数名称:items - Error in ASP.NET MVC SelectList. Value Cannot be null, parameter name: items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM