 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 5th, 2007, 10:43 AM
|
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Paging in gridview control
I am having trouble with paging in gridview control.
GridView was created in runtime:
System.Web.UI.WebControls.GridView gvProgrm = new GridView();
gvProgrm.AllowSorting = true; //(Sorting works perfectly)
gvProgrm.AllowPaging = true;
gvProgrm.PageIndex = 0;
gvProgrm.PagerSettings.FirstPageText = "First";
gvProgrm.PagerSettings.LastPageText = "Last";
gvProgrm.PagerSettings.NextPageText = "Next";
gvProgrm.PagerSettings.PreviousPageText = "Previous";
gvProgrm.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
gvProgrm.PagerSettings.Position = PagerPosition.Bottom;
gvProgrm.PagerSettings.Visible = true;
gvProgrm.PageSize = 20;
BuildColumnsDynamically(); // creates and adds columns dynamically
gvProgrm.Visible = true;
gvProgrm.DataSource = ds;
gvProgrm.DataBind();
private void InitializeComponent()
{
this.gvProgrm.Sorting += new GridViewSortEventHandler(this.gvProgrm_Sorting);
this.gvProgrm.PageIndexChanging += new GridViewPageEventHandler(this.gvProgrm_PageIndexCh anging);
this.Load += new System.EventHandler(this.Page_Load);
}
protected void gvProgrm_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//GridView1.DataSource = SortDataTable(GridView1.DataSource as DataTable, true);
gvProgrm.DataSource = SortDataTable(dt, true);
gvProgrm.PageIndex = e.NewPageIndex;
gvProgrm.DataBind();
}
When I set to AllowPaging = true, I get the error:
Object reference not set to an instance of an object.Object reference not set to an instance of an object.
Please advise.
Thanks.
|
|

July 5th, 2007, 10:52 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
the code looks good.. I don't know why you have an error there.. what happens if you invert the allowsorting and allowpaging.. the error is in the same place???
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

July 5th, 2007, 03:13 PM
|
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
When gvProgrm.AllowPaging = false - it works fine.
|
|

July 6th, 2007, 01:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It would be very useful to find out what part of the code throws the object reference exception. Otherwise, it's like searching for a needle in a haystack.
Try setting a break point of the line that creates the GridView and then step through it line by line to see where it breaks.
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
|

July 6th, 2007, 07:30 AM
|
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
already stepped through the code, it brakes on:
gvProgrm.DataBind();
with error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 85:
Line 86: gvProgrm.DataSource = ds;
Line 87: gvProgrm.DataBind();
Line 88:
Line 89: Panel1.Controls.Add(gvProgrm);
Source File: c:\inetpub\wwwroot\ETM_Queue\Grid.aspx.cs Line: 87
|
|

July 6th, 2007, 01:56 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
If this doesn't break:
gvProgrm.DataSource = ds;
then it stands to reason that gvProgrm isn't null.
Are you sure ds is not null? That's the only other thing that could be causing this error.
- Peter
|
|

July 6th, 2007, 03:04 PM
|
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for replying Peter,
dataset is OK - it has data,
also, the error happend when AllowPaging is set to true
|
|

July 6th, 2007, 08:00 PM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Vadim, pay attention to your data source. I suppose you have a complex object for it that is not completely restored.
Try to debug some binding event handler to see all ds properties bound. I can suppose some of them will be null.
|
|

February 17th, 2010, 02:12 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Vadim - ever figure this out?
Quote:
Originally Posted by VadimZ
Thanks for replying Peter,
dataset is OK - it has data,
also, the error happend when AllowPaging is set to true
|
I know from having gone through the same steps with debugger that
1) The nullexception on databind() error can happen when Datasources are fine ie... not null... and
2) the gridvew is fine
In a very simple class I allocate a gridview [gridview grd = new gridView()] , then setting css class properties, data source etc. With paging off - NO problem. Paging on - null exception.
Sorry I dont have a solution yet. It probably has something to do with the paging even not being handled correctly... but its late and Im to tired to deal with it right now. Not to mention that I'm lazy.
Did you find that it had to do with how you handled paging event?
|
|
 |