Take the 2-minute tour ×

I am using a standart .net 2.0 Gridview which uses an XMLDatasource to populate the Grid. The Data property of the XMLDatasource is set dynamically which allows the gridview to change based on input.

All this works fine however I am having problems with paging...

I have set the AllowPaging Property to "true" and set the PageSize Property to "10". The GridView populates fine the first time around showing the first 10 records and the number of pages as hyperlinks at the bottom, BUT when i try to click on any of the page numbers to view them a message box pops up saying "Object reference not set to an instance of an object"

any ideas what I'm doing wrong?? or is there anything i need to do which i have missed out on??

Code currently being used;


<asp:GridView ID="GridView1" 
<SelectedRowStyle BackColor="Red" />
   <asp:BoundField DataField="TYPE" HeaderText="TYPE" SortExpression="TYPE" />
   <asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION" SortExpression="DESCRIPTION" />


<asp:XmlDataSource ID="XmlDataSource1" runat="server" TransformFile="~/XML/grid2.xslt" EnableCaching="False">

vb.net code which sets the Data property of the XMLDatasource...

Private Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click

  XmlDataSource1.Data = _testLib.GetGridXML(_Num)
End Sub

where _testLib.GetGridXML is a function that returns an XML string based on the _Num passed in.

share|improve this question
Either assign your data on Page_Load all the time or on every secondary event, i.e. grid sorting, page index change etc (anything that changes grid's state). See my updated answer... –  Ruslan Apr 6 '09 at 15:40
add comment

3 Answers

up vote 1 down vote accepted

It's difficult to say without seeing your code... I would speculate that you assign the Data conditionally, i.e:

If Not IsPostBack Then
   MyXMLDataSource.Data = "...some xml..."
End If

In this case it will be empty on post back and you get your exception. Could be something else, but then again, no code...


Since you've added more information...

You must have something like code above on Page_Load. Since you are not providing it here, I presume you do. If you don't, you'd get the null reference exception on each load.

With that in mind, you assign data on some button click, but not on PageIndexChanging.

You click the button, the page loads, you assign the data, the grid shows it. Then you click the grid's next link, the page loads again, PageIndexChanging gets fired, your click event doesn't -- where's assignment then?

From what I see, either assign the Data property on Page_Load every time or do it in all subsequent events, i.e. on page change, on sort, etc.

Btw, you don't have to call DataBind when assigning XmlDataSource declaratively.

share|improve this answer
I have incldued the current code being used in the question –  eMTeeN Apr 6 '09 at 10:48
Thanks! that does make sense and WORKED :) one thing to mention though, If Viewstate is enabled on the Grid it works better if the Data property is set in PreRender event as opposed to page load. However If ViewState is Disabled it works fine if the Data property is set in the PageLoad event! –  eMTeeN Apr 6 '09 at 16:12
add comment

It should work if you do your databinding on the PreRender event

Since the XML datasource is being set dynamically if you set it on the PageLoad all the page elements might not exist at this stage.

share|improve this answer
add comment

Are you implelenting the OnPageChanging Event ? Normally you need to implement it and use the e.NewPageIndex property from the Event Argument to set it in your gridview.

share|improve this answer
You don't need to do that if the data source is assigned declaratively, which I presume is the case... –  Ruslan Apr 3 '09 at 19:22
Btw, if the OnPageChanging wasn't implemented, it would give a different error, specifically stating that it was fired and not handled. –  Ruslan Apr 3 '09 at 19:23
add comment

Your Answer


