Question

I am facing issue in binding the gridview when one of the tag in the XML nodelist is missing. I am getting below error message:

A field or property with the name 'SUMMARY' was not found on the selected data source.

Please someone help me with how to handle the missing values during binding so that I can display custom text message such as: "No Summary Available"

I am using .NET version 3.5 and binding the GridView from XML data.

ASPX Code:

            <asp:GridView ID="gvSystemX" runat="server" AutoGenerateColumns="False" AlternatingRowStyle-CssClass="even"
        CellPadding="4" Font-Names="Tahoma" Font-Size="Small" ForeColor="#333333" GridLines="None" EmptyDataText="Mayur">
        <RowStyle BackColor="#E3EAEB" />
        <Columns>
            <asp:BoundField HeaderText="TITLE" DataField="AZKTITLE" />
            <asp:BoundField HeaderText="DESCRIPTION" DataField="AZKSUMMARY" />
            <asp:BoundField HeaderText="SOURCE" DataField="AZKSOURCE" />
            <asp:BoundField HeaderText="DATABASE NAME" DataField="DREDBNAME" />
        </Columns>
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <AlternatingRowStyle CssClass="even"></AlternatingRowStyle>
    </asp:GridView>

Code Behind:

In the response object I am getting the XML data...

            XmlNamespaceManager nsMgr = new XmlNamespaceManager(response.NameTable);
            nsMgr.AddNamespace("autn", "http://schemas.autonomy.com/aci/");

            nodeList = response.SelectNodes("autnresponse/responsedata/autn:hit/autn:content/DOCUMENT", nsMgr);
            gvSystemX.DataSource = ConvertToDataTable(nodeList);
            gvSystemX.DataBind();
Was it helpful?

Solution

This error is becuase the DataTable´s DataRow doesn´t have the column summary (AZKSUMMARY) of course. This can be solved in your ConvertToDataTable method. In there I assume you are creating column for all existing nodes.

You must create all 4 columns for every row even if it not exists in the xml file. If it not exits you can add the value you want, for example "No Summary Available".

Even better would be to implement a object with 4 properties and put them in list. But stick to the datatable if you like it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top