System.Data.DataRowView'不包含名为'eventDate'的属性

时间:2015-07-27 06:15:30

标签: asp.net c#-4.0 gridview

当在sql中使用此查询时,它工作正常,但在尝试在c#中使用它时,它说:DataBinding:'System.Data.DataRowView'不包含名为'eventDate'的属性。这个错误意味着什么?

  SqlCommand cmd = new SqlCommand("SELECT DATEPART(yyyy, eventDate)  FROM events group by eventDate", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        ddlLocation.DataSource = dt;
        ddlLocation.DataTextField = "eventDate";
        ddlLocation.DataValueField = "eventDate";
        ddlLocation.DataBind();
        ddlLocation.Items.Insert(0, new ListItem("All", ""));

Gridview

 protected void BindGridview()
{
    DataTable dt = new DataTable();

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    SqlCommand com = new SqlCommand();
    con.Open();
    SqlCommand cmd = new SqlCommand("select id, eventDate, eventName from events ", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    con.Close();
    gvDetails.DataSource = dt;
    gvDetails.DataBind();

}

1 个答案:

答案 0 :(得分:4)

尝试使用相同的代码,但使用此查询可能会有效

SqlCommand cmd = new SqlCommand("SELECT DATEPART(yyyy, eventDate) AS eventDate FROM events group by eventDate", con);

更改选择查询的别名:)