DropDownList causes error: Object reference not set to an instance of an object

asp.net c# drop-down-menu entity-framework visual-studio-2010

Question

This mistake keeps occurring, and I have no clue why. I've been searching Google and the asp.net website for two days, so I need some assistance. The mistake is:

Object reference not set to an instance of an object

Below is my code:

DropDownList DropDownList1 =(DropDownList)ListView1.InsertItem.FindControl("DropDownList1");

string highToLow = DropDownList1.SelectedValue;
string lowToHigh = DropDownList1.SelectedValue;

if (highToLow == "1")
{
    var exmapleFilter = from users in testEntities.users 
                        orderby users.id descending
                        select users;

    ListView1.DataBind();
}

I want to execute the ADO.net Entity Framework code to return a sorted list of data when the chosen index changes. I have set the value of the drop down to 1 for high and 2 for low.

To display the contents of my database, I'm presently utilizing a list view and a linq data source.

Thanks.

EDIT:

The stack trace is shown below.

   System.NullReferenceException was unhandled by user code
   Message=Object reference not set to an instance of an object.
   Source=App_Web_s0ked5y3

   StackTrace:
   at Default.DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
   in Default:line 120

   at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)

   at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent()

   at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.
   RaisePostDataChangedEvent()

   at System.Web.UI.Page.RaiseChangedEvents()

   at System.Web.UI.Page.ProcessRequestMain(Boolean 
   includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   InnerException: 
1
3
7/5/2012 8:20:15 PM

Accepted Answer

Verify that you have a list view.

<InsertItemTemplate>
   ....          
   <asp:DropDownList ID="DropDownList1" runat="server" />
   .... 

In connection with

ListView1.InsertItem.FindControl("DropDownList1");
3
7/31/2012 8:35:08 AM

Popular Answer

When using a reference variable with a null value, you often get the error "Object reference not assigned to an instance of an object." It indicates that there isn't an equivalent item on the heap.

Due diligence should thus be taken to double-check your template and load all data during the pageload event.

Hope it's useful.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow