Asp.net 数据绑定:';System.Data.DataRowView';不包含名为';名称';

Asp.net 数据绑定:';System.Data.DataRowView';不包含名为';名称';,asp.net,Asp.net,Asp.code <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1" DataTextField="socialname"> <asp:ListItem text="select">Commen

Asp.code

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1" DataTextField="socialname">
    <asp:ListItem text="select">Comment using</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:prakashConnectionString %>" SelectCommand="SELECT [socialname] FROM [socialnetwork]"></asp:SqlDataSource>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

您尚未分配
数据源

DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "socialname";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
但是您同时使用了aspx上的
DataSourceID
,以及codebehind
DataSet
。对于
SqlDataSource
,您没有选择名称,只选择了:

SELECT [socialname] FROM [socialnetwork]
只需删除
SqlDataSource
,它只是多余的

另外,我建议对连接和dataadapter使用
using
-语句,以确保释放所有非托管资源并关闭连接(即使出现错误):


我在使用asp.net列表框时遇到了类似的问题。我更改了返回到显示值字段中的列的名称,以匹配错误条件中的名称,从而解决了问题。例如,我遇到以下错误:

[HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'DisplayValue'.]
从SELECT语句返回的值保存在名为theValue的列中。我将列别名改为DisplayValue:

SELECT theValue as DisplayValue
FROM whateverTable
注:我在VS 2012工作

[HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'DisplayValue'.]
SELECT theValue as DisplayValue
FROM whateverTable