views:

44

answers:

1

Hi guys, I'd really appreciate your time to look at this. I am trying to use GridView and SPDataSource within a SharePoint Application Page (code behind).

This is what I have in the Application page: (Default.aspx)

<SharePoint:SPDataSource id="oSPDS" runat="server" DataSourceMode="List" UseInternalName="true">
</SharePoint:SPDataSource>

<asp:GridView ID="oGrid" DataSourceID="oSPDS" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Request ID" />
    </Columns>
</asp:GridView>

And this is what's in the code behind page: (Default.aspx.cs)

public class Default : Page
{
    protected GridView oGrid;
    protected SPDataSource oSPDS;

    protected void Page_Load(object sender, EventArgs e)
    {
        SPWeb web = SPContext.Current.Web;
        this.oSPDS = new SPDataSource();
        string strWebID = web.ID.ToString();
        string strListID = web.Lists["PQR"].ID.ToString();
        this.oSPDS.SelectParameters.Add(new Parameter("WebID", TypeCode.String, strWebID));
        this.oSPDS.SelectParameters.Add(new Parameter("ListID", TypeCode.String, strListID));
        this.oSPDS.List = web.Lists["PQR"];
        this.oSPDS.DataSourceMode = SPDataSourceMode.List;
        this.oSPDS.SelectCommand = "<Query><Where><Eq><FieldRef Name='Archive'/><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy></Query>";

        this.oGrid = new SPGridView();
        this.oGrid.AutoGenerateColumns = false;
        this.oGrid.AutoGenerateDeleteButton = true;
        this.oGrid.DataSource = oSPDS;
        this.oGrid.DataBind();
    }

I'm getting the following error message.

Object reference not set to an instance of an object.   at Microsoft.SharePoint.WebControls.SPDataSourceView.ExecuteSelect(DataSourceSelectArguments selectArguments) 

Can someone please help me figure out what I'm doing wrong? I think I am not initializing something that's required by "SPDataSourceView.ExecuteSelect". But I can't seem to connect the dots to find what's missing.

A: 

After playing with Datagrid, GridView & Repeater I've decided to use Repeater with PagedDataSource. This is the only option that gives you complete control over the generated HTML and let's you enjoy the full potential of JQuery/JSON.

  • Thanks, Karthik.
Karthik