【问题标题】:DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'name'DataBinding:“System.Data.DataRowView”不包含名为“name”的属性
【发布时间】:2014-09-15 09:41:27
【问题描述】:

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>

C#代码

SqlConnection con = new SqlConnection("****");
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Bindddl();
        BindTitle();
        Label3.Text = DropDownList1.Items[0].Text;
    }
}

protected void Bindddl()
{

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from socialnetwork", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DropDownList1.DataTextField = "socialname";
    DropDownList1.DataValueField = "name";
    DropDownList1.DataBind();
    con.Close();
}

protected void BindTitle()
{
    if (DropDownList1 != null)
    {
        foreach (ListItem li in DropDownList1.Items)
        {
            li.Attributes["title"] = "social/" + li.Value; // it ll set the value of items in dropdownlist as tooltip

        }
    }
}

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
    BindTitle();
    Label3.Text = DropDownList1.SelectedItem.Text;
    Label3.Text = "Commented by";
}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    你还没有分配DataSource:

    DropDownList1.DataSource = ds.Tables[0];
    DropDownList1.DataTextField = "socialname";
    DropDownList1.DataValueField = "name";
    DropDownList1.DataBind();
    

    但是你已经使用了两个,aspx 上的DataSourceID 和代码隐藏DataSet。对于SqlDataSource,您没有选择名称,而只是:

    SELECT [socialname] FROM [socialnetwork]
    

    只需删除SqlDataSource,它就是多余的。

    顺便说一句,我建议对连接和数据适配器使用using-statement,以确保所有非托管资源都已被释放并关闭连接(即使出现错误):

    DataSet ds = new DataSet();
    using(SqlConnection con = new SqlConnection("****"))
    using(SqlDataAdapter da = new SqlDataAdapter("select * from socialnetwork", con))
        da.Fill(ds);  // you don't need to open/close the connection with Fill
    DropDownList1.DataSource = ds.Tables[0];
    // ...
    

    【讨论】:

      【解决方案2】:

      我在使用 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 中工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多