Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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
Updated 1-Oct-12 23:42pm
v3

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,
C#
(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:
C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900