Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I have a dropdownlist inside gridview in edit item template.I have binded the details in row data bound event.Here is my code:
C#
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
       {
           try
           {
               if (e.Row.RowType == DataControlRowType.DataRow)
               {
                   //DropDownList ddlyear = (DropDownList)e.Row.FindControl("DropDownList4");
                   DropDownList ddlyear = (DropDownList)e.Row.FindControl("DropDownList4");
                   int year = System.DateTime.Now.Year;
                   for (int intCount = 1900; intCount <= year; intCount++)
                   {
                       ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));
                   }
               }
           }
           catch (Exception ex)
           {
               spanid.InnerHtml = ex.Message;
           }
       }

Iam getting error in this line " ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));". Error is "Object Reference not set to an instance of the object".CAn anyone pls tell me what is the error in the above code.In a single Edit item template,I have kept 2 dropdown and one textbox.Thanks in Advance
Posted
Updated 23-Aug-12 0:39am
v2

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[^]

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.


For now, based on what you say "Iam getting error in this line " ddlyear.Items.Add(new ListItem(intCount.ToString(), intCount.ToString()));"."
Since intCount is initialized, it cannot be null. Only reason looks like ddlyear object must be NULL. This means, there was no 'DropdownList4' in the row here. Check it and resolve.
 
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