开发者

DataBinding: 'System.Data.DataRowView' does not contain a property with the name

I am getting this weird error... the primary key in my database is 'DocumentID' so I know that is not the issue. I am trying to get the select,edit & delete gridview buttons to work but I need the datakeynames to be set correctly for them to be available to use. any ideas?

<asp:GridView ID="GridView1" runat="server" DataSourceID="sdsDocuments" EnableModelValidation="True"
        SelectedIndex="0" OnSelectedIndexChanged="GridV开发者_StackOverflow中文版iew1_SelectedIndexChanged" DataKeyNames="DocumentID, DocumentTitle, DocumentBody">
        <Columns>
            <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="DocumentTitle" HeaderText="DocumentTitle" SortExpression="Title" />
            <asp:BoundField DataField="DocumentBody" HeaderText="DocumentBody" SortExpression="Body" />
            <asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
        SelectCommand="SELECT [DocumentTitle], [DocumentBody] FROM [tblDocument]" />

Here is the stack trace...

    [HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a                 property with the name 'DocumentID'.] 
        System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +8672869
       System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2178
       System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
       System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
       System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       System.Web.UI.WebControls.GridView.DataBind() +4
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


Well, you haven't selected the documentid column and hence it's not present in either datatable or dataview which you are binding to grid OR referencing that column through datatable.

Change your query to

 SelectCommand="SELECT [DocumentID],[DocumentTitle], [DocumentBody] FROM [tblDocument]" /> 


1- First step you have to select the Documentid column in SELECT statement

SelectCommand="SELECT [DocumentID], [DocumentTitle], [DocumentBody] FROM [tblDocument]" />

2- You have to include it in datagridview COLUMN as you did:

<asp:GridView ID="GridView1" runat="server" DataSourceID="sdsDocuments" EnableModelValidation="True"
        SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="DocumentID, DocumentTitle, DocumentBody">
        <Columns>
            <asp:BoundField DataField="DocumentID" HeaderText="DocumentID" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="DocumentTitle" HeaderText="DocumentTitle" SortExpression="Title" />
            <asp:BoundField DataField="DocumentBody" HeaderText="DocumentBody" SortExpression="Body" />
            <asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>

3- If you are using Paging in datagridview AllowPaging="True" ,you have to SELECT also documentid on the event OnSelectedIndexChanged="GridView1_SelectedIndexChanged" to SELECT data in second and third page and so on:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="sdsDocuments" EnableModelValidation="True"
        SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="DocumentID, DocumentTitle, DocumentBody">

Then on the event OnSelectedIndexChanged="GridView1_SelectedIndexChanged" :

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
                DataTable dt = // your SELECT statement //
                GridView1.DataSource = dt;
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataBind();
}

if you dont use this void then you will get same error when you click page 2


It seems that your sqldatasource does not return the DocumentId column.

Maybe you should change the datasource definition to this:

<asp:SqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
    SelectCommand="SELECT [DocumentID], [DocumentTitle], [DocumentBody] FROM [tblDocument]" />


DataSet Année = new DataSet();
Année.ReadXml(Server.MapPath("~/Annees.xml"));

DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "Number";
DropDownList1.DataSource = "Année";

DropDownList1.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜