473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

GridVew Error, Object reference not set to an instance of an object.

My page has several GridView controls placed on it from the designer.
My datasource is dynamic (DataTable) assigned in the code behide.

On this same page I am creating a GridView control from the code behide and placing it in an asp:table, assigning the properties and styles to duplicate the designer GridViews. Everything works, displays good and column sorting works.

However. If the datasource contains more records then the GridView PageSize I receive the Object reference error. I have tried everything I can think of to no avail. If my datasource contains 30 records and my page size is default 10 or anything less than 30 I will receive the error. If I change the page size to 30 or greater it works fine (No pageing of course) The boolean AllowPaging is set to true. The paging on the GridViews placed on the page from the designer works fine. Just the dynamic GridView causes the error. The datasource for all of the GirdViews are obtained and assigned in the same fashion.

Any suggestions would be greatly appreciated.
May 18 '07 #1
2 7407
Frinavale
9,735 Expert Mod 8TB
My page has several GridView controls placed on it from the designer.
My datasource is dynamic (DataTable) assigned in the code behide.

On this same page I am creating a GridView control from the code behide and placing it in an asp:table, assigning the properties and styles to duplicate the designer GridViews. Everything works, displays good and column sorting works.

However. If the datasource contains more records then the GridView PageSize I receive the Object reference error. I have tried everything I can think of to no avail. If my datasource contains 30 records and my page size is default 10 or anything less than 30 I will receive the error. If I change the page size to 30 or greater it works fine (No pageing of course) The boolean AllowPaging is set to true. The paging on the GridViews placed on the page from the designer works fine. Just the dynamic GridView causes the error. The datasource for all of the GirdViews are obtained and assigned in the same fashion.

Any suggestions would be greatly appreciated.
You've likely tried this already but have you set the GridView's paging property to allow paging?

Edit: Errr...sorry should have read the whole message...This sounds a bit crazy.

Double check the allow paging again though just to be sure.

Could you post the code that creates your GridView and data source please?
May 18 '07 #2
Data Source for the GridView is a System.Data.DataTable with proper field names and types.

As soon as the gv.DataBind() is executed I receive the Error.
The gvInvoices_DataBound event does not fire.

GridView Source:
Expand|Select|Wrap|Line Numbers
  1.       gv = new System.Web.UI.WebControls.GridView();
  2.       gv.ID = "gvInvoice" + aPoints[i];
  3.       gv.AllowPaging = true;
  4.       gv.AllowSorting = true;
  5.       gv.AutoGenerateColumns = false;
  6.       gv.BackColor = System.Drawing.Color.White;
  7.       gv.BorderColor = System.Drawing.Color.FromArgb(231,231,255);
  8.       gv.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
  9.       gv.BorderWidth = new System.Web.UI.WebControls.Unit("1px");
  10.       gv.CellPadding = 3;
  11.       gv.ShowFooter = true;
  12.       gv.PageSize = 50;
  13.       // GridView Columns
  14.       gvc = new System.Web.UI.WebControls.BoundField();
  15.       gvc.DataField = "invoice_number";
  16.       gvc.HeaderText = "Number";
  17.       gvc.ReadOnly = true;
  18.       gvc.SortExpression = "number";
  19.       gvc.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  20.       gv.Columns.Add(gvc);
  21.       gvc = new System.Web.UI.WebControls.BoundField();
  22.       gvc.DataField = "amount";
  23.       gvc.HeaderText = "Amount";
  24.       gvc.ReadOnly = true;
  25.       gvc.SortExpression = "amount";
  26.       gvc.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  27.       gv.Columns.Add(gvc);
  28.       // Grid View Styles
  29.       gv.RowStyle.BackColor = System.Drawing.Color.FromArgb(231,231,255);
  30.       gv.RowStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  31.       gv.RowStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  32.       gv.RowStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("8pt");
  33.       gv.SelectedRowStyle.BackColor = System.Drawing.Color.FromArgb(115,138,156);
  34.       gv.SelectedRowStyle.Font.Bold = true;
  35.       gv.SelectedRowStyle.ForeColor = System.Drawing.Color.FromArgb(247,247,247);
  36.       gv.PagerStyle.BackColor = System.Drawing.Color.FromArgb(231,231,255);
  37.       gv.PagerStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  38.       gv.PagerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
  39.       gv.PagerStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  40.       gv.PagerStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("9pt");
  41.       gv.HeaderStyle.ForeColor = System.Drawing.Color.FromArgb(247,247,247);
  42.       gv.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(74,60,140);
  43.       gv.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
  44.       gv.HeaderStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  45.       gv.HeaderStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("9pt");
  46.       gv.HeaderStyle.Font.Bold = true;
  47.       gv.HeaderStyle.Wrap = true;
  48.       gv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromArgb(247,247,247);
  49.       gv.FooterStyle.BackColor = System.Drawing.Color.FromArgb(181,199,222);
  50.       gv.FooterStyle.ForeColor = System.Drawing.Color.FromArgb(74,60,140);
  51.       gv.FooterStyle.Font.Names = new string[] { "Tahoma","Arial","Verdana","Trebuchet","Sans-Serif" };
  52.       gv.FooterStyle.Font.Size = new System.Web.UI.WebControls.FontUnit("8pt");
  53.       gv.DataBound += new System.EventHandler(gvInvoices_DataBound);
  54.       gv.PageIndexChanging += new System.Web.UI.WebControls.GridViewPageEventHandler(gvInvoices_PageIndexChanging);
  55.       gv.Sorting += new System.Web.UI.WebControls.GridViewSortEventHandler(gvInvoices_Sorting);
  56.       gv.DataSource = tblTmp1;
  57.       gv.DataBind();
  58.  
May 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
18
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12,...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
3
by: SAL | last post by:
I am getting the following ERROR in my WebApp on line 30: Server Error in '/TestWebApp' Application. -------------------------------------------------------------------------------- Object...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.