Wrox Programmer Forums
|
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
 
Old July 5th, 2007, 10:43 AM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.



 
Old July 5th, 2007, 10:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
================================================== =========
 
Old July 5th, 2007, 03:13 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default


When gvProgrm.AllowPaging = false - it works fine.

 
Old July 6th, 2007, 01:51 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old July 6th, 2007, 07:30 AM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old July 6th, 2007, 01:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old July 6th, 2007, 03:04 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for replying Peter,
dataset is OK - it has data,
also, the error happend when AllowPaging is set to true


 
Old July 6th, 2007, 08:00 PM
Registered User
 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old February 17th, 2010, 02:12 AM
Registered User
 
Join Date: Feb 2010
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Red face Vadim - ever figure this out?

Quote:
Originally Posted by VadimZ View Post
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?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Paging problem in gridview kalyanykk ASP.NET 3.5 Basics 1 December 8th, 2008 03:33 AM
How to show paging outside GridView control. rao965 ASP.NET 2.0 Basics 2 June 16th, 2008 03:50 AM
Sorting and Paging of gridview lily611 C# 2005 0 April 24th, 2008 03:09 AM
Chapter 1-GridView Paging Carl Tseng BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 November 25th, 2007 06:56 AM
Sorting and Paging GridView using Alphabets yukijocelyn ASP.NET 2.0 Basics 9 August 29th, 2007 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.