-2

I get NullReferenceException was unhandled by usercode in below code.

if (item["Area"] != null)
{
    drpArea.Items.FindByText(item["Area"].ToString()).Selected = true;//error occurs here.
}

Tried to figure out a million times.But i am unable to do.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
missReclusive
  • 105
  • 1
  • 4
  • 20
  • 2
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Apr 13 '12 at 04:17
  • 2
    i think you need to test for null in drpArea.Items.FindByText(item["Area"].ToString()) as well. Maybe even the Items property of the drpArea. I dont know the type of that variable reference. – Roman Apr 13 '12 at 08:59

1 Answers1

1

may be you should first get listitem from Dropdownlist and than set the selected value

Try this :

 ListItem li = drpArea.Items.FindByText(Convert.ToString(item["Area"]));
 if (li != null)
    drpArea.SelectedValue = li.Value;
Jignesh Rajput
  • 3,538
  • 30
  • 50