Click here to Skip to main content
13,198,563 members (37,590 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 27 Mar 2010

GridView Pagination issue when dynamically added….!

, 27 Mar 2010
Rate this:
Please Sign up or sign in to vote.
NullReferenceexception issue for gridview pagination

Hello ASP.NET developers… Today I came across a small issue which killed a large amount of my development time. The issue was a little tricky between the lines of code. I have an ASP.NET gridview which is dynamically (on the fly) created on my page. When I am inserting 2 lines of code  just for allowing the gridview pagination, code breaks at runtime giving  a NullReferenceException.

Cause of the issue: I was adding the dynamically created gridview(object) into page’s control collection after the databinding. This works fine if there is no pagination code added. But if there is code which allows pagination for the dynamically added gridview, this gridview should be added to the page’s control collection before the databinding.

Please see the below mentioned code blocks for the difference.

This piece of code will give the Null Reference Exception:

GridView gvDepartment = new GridView ();
gvDepartment.ID = "gvDepartment";

gvDepartment.AllowPaging = true;
gvDepartment.PageSize = 4;

gvDepartment.DataSource = GetDepartmentRows ();
gvDepartment.DataBind ();

this.form1.Controls.Add ( gvDepartment );

This is the perfect code:

GridView gvDepartment = new GridView ();
gvDepartment.ID = "gvDepartment";

gvDepartment.AllowPaging = true;
gvDepartment.PageSize = 4;

/* Need to add gridview into controls collection before databind,if there is pagination */
this.form1.Controls.Add ( gvDepartment );

gvDepartment.DataSource = GetDepartmentRows ();
gvDepartment.DataBind ();

I hope this post helped you or has given a nice thought. Thanks for reading. Please comment if you feel that there are any issues.

Please have a look into my blog for more posts:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

abin jaik
Web Developer
India India
I am Abin Jaik Antony, working as a Software Developer.My technology background area extends to MOSS 2007,Microsoft BI,.NET Framework etc.
Visit my blog for more details

You may also be interested in...

Comments and Discussions

GeneralNice info shared... Pin
Sandeep Mewara27-Mar-10 3:40
memberSandeep Mewara27-Mar-10 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 27 Mar 2010
Article Copyright 2010 by abin jaik
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid