I keep getting this error when I implement my code for a dropdown list.
Here is my code:
String sort = "";
String day_hire = "Daily hire rate";
String man_date = "Manufacturing date";
string man_n = "Manufacturer's name";
if (sl2.SelectedValue.ToString()== man_n)
{
sort = "man.manufacturer_name";
}
else if (sl2.SelectedValue.ToString() == man_date)
{
sort = "veh.manufacturing_date";
}
else if (sl2.SelectedValue.ToString() == day_hire)
{
sort = "veh.daily_hire_rate";
}
else
{
sort = "veh.daily_hire_rate";
}
this is in constructor. In view I’ve got:
<%=Html.DropDownList ("sort",((SelectList)ViewData["selectOptions2"]))%>
it’s a runtime error that says sl2.SelectedValue.ToString() is null. Please help
When you do a POST, the selected value will be stored in the “sort” value, which you had declared here:
So you need to do:
This is a very good article which goes into more detail.