views:

104

answers:

2

See ,I have drop down control and i have keep the LIST as datasource. But if list is null Then it throw null exception. So what is the standard way to handle this situation?

A: 

Bind the list to the dropdown after checking whether the list is null or not.

if (list != null) {
   dd.DataSource = list;
}
else {
   dd.DataSource = new List<ObjType>();
}

Note: ObjType is the type of list items that you are using in the dropdown, for example string, if the list that you are using is a list of strings.

Thanks

Mahesh Velaga
Also, if using version 3.5 of the .NET Framework, the Enumerable.Empty<TResult>() method is ideal for creating an empty IEnumerable<T> instance.
Programming Hero
+4  A: 

If you can, keep the list as an empty list rather than a null list. It's easy to confuse the usage of an empty list with a null list (what do each of these mean), and if you use empty lists consistently then you'll reduce the opportunity for null pointer exceptions.

Brian Agnew