Click here to Skip to main content
13,299,727 members (68,051 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi every one...

in my web application, i have a gridview,in that gridview footer template i have a dropdownlist. I need to load the values in DB table on pageload.

but am not using the row_data_bound() here.

my code is below:
private void bind_gridview_with_dropdownlist()
    SqlDataAdapter adp = new SqlDataAdapter("select UOM from Tbl_Com_Single where Flag='Bus'", con);
    DataSet ds = new DataSet();
    adp.Fill(ds, "Tbl_Com_Single");
    bind_dropdownlist = (DropDownList)gvDetails.FooterRow.FindControl("DropftrTType");
    bind_dropdownlist.DataSource = ds;
    bind_dropdownlist.DataValueField = "UOM";
    bind_dropdownlist.Items.Insert(0, new ListItem("Select", "Default value"));

its working fine in localhost and IIS also.

but when publishing it shows the following error:
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 
Line 303:        DataSet ds = new DataSet();
Line 304:        adp.Fill(ds, "Tbl_Com_Single");
Line 305:        bind_dropdownlist = (DropDownList)gvDetails.FooterRow.FindControl("DropftrTType");
Line 306:        bind_dropdownlist.DataSource = ds;
Line 307:        bind_dropdownlist.DataValueField = "UOM";
Source File: c:\inetpub\vhosts\\httpdocs\Bus\Entries.aspx.cs    Line: 305 
Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   Entries.bind_gridview_with_dropdownlist() in c:\inetpub\vhosts\\httpdocs\Bus\Entries.aspx.cs:305
   Entries.BindGridView() in c:\inetpub\vhosts\\httpdocs\Bus\Entries.aspx.cs:319
   Entries.Page_Load(Object sender, EventArgs e) in c:\inetpub\vhosts\\httpdocs\Bus\Entries.aspx.cs:58
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

getting error in 305 line

Please help me is possible to solve...
give me the solution..

thanks in advance

Posted 1-Oct-12 20:53pm
Updated 2-Oct-12 0:42am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

For now,

Most probably, FindControl returned null. Hence the error. You need to make sure such control(DropftrTType) exists in FooterRow.

2 things:
1. Do:
bind_dropdownlist = gvDetails.FooterRow.FindControl("DropftrTType") as DropDownList;

This will handle the conversion object reference error. Even if findcontrol returns null, it will not throw an error while conversion to dropdownlist

2. Make sure findcontrol is accessing correct control and returns what is expected.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100