Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i added on dropdownlist in grid edititemtemplate, when i am binding the data in .cs file with the event rowdataboud then i got the error
System.NullReferenceException: Object reference not set to an instance of an object.


here is my sample cs code
C#
protected void gvTestType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus");
               DataSet ds = new DataSet();
               StatusBll sBl = new StatusBll();
                ds = sBl.SelectStatusDetails();
                ddl.DataTextField = "Description";//the source of your dropdown
                ddl.DataValueField = "StatusId";
                ddl.DataSource = ds.Tables["Status"];
                ddl.DataBind();
            }
        }
Posted
Updated 14-Jun-12 3:13am
v3
Comments
Dev-0001 14-Jun-12 9:41am    
what's the error message

Could be that your
sBl.SelectStatusDetails()
tries to get a variable from something that is null.

Or
ds.Tables["Status"]
doesn't exist...
 
Share this answer
 
v2
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[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Based on the code snippet shared, potential places:
C#
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus"); //ddlStatus was not found and explicit cast threw an error

C#
ds = sBl.SelectStatusDetails(); // ds returned is null or empty, assuming no error in method called!

C#
ddl.DataSource = ds.Tables["Status"]; // there is no table named 'Status' in the dataset ds

Check, find and fix it.
 
Share this answer
 
v2

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