Click here to Skip to main content
11,484,402 members (66,130 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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.DataBind();
        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\rktmithra.in\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\rktmithra.in\httpdocs\Bus\Entries.aspx.cs:305
   Entries.BindGridView() in c:\inetpub\vhosts\rktmithra.in\httpdocs\Bus\Entries.aspx.cs:319
   Entries.Page_Load(Object sender, EventArgs e) in c:\inetpub\vhosts\rktmithra.in\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


regards,
Raaj
Posted 1-Oct-12 20:53pm
Edited 2-Oct-12 0:42am
v3

1 solution

Rate this: bad
good
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,
(DropDownList)gvDetails.FooterRow.FindControl("DropftrTType");
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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 170
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,199
3 Maciej Los 2,681
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2015
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