Click here to Skip to main content
Sign Up to vote bad
good
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 - 19:53
Edited 1 Oct '12 - 23:42


1 solution

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
Your Filters
Interested
Ignored
     
0 Mohammed Hameed 268
1 Sergey Alexandrovich Kryukov 238
2 OriginalGriff 236
3 Mayur_Panchal 153
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid