Saturday, April 30, 2011

ASP.NET Drop down list and strongly typed DataTable

I had code where I populate the contents of an ASP.NET DropDownList with a strongly typed DataTable returned from an ASP.NET web service. In this code I bound the DataTable to the DataSource of the DropDownList and did a DataBind() in the Page_Load() method. This worked perfectly fine on my local box.

However, when I loaded up the ASP.NET page in the production data center, I got:

System.Web.HttpException: A
DropDownList cannot have multiple items selected

To solve this problem, after I bound the DataTable to the DropDownList, I iterated through each ListItem and set its Selected property to false:

foreach (ListItem myItem in myDropDownList)
{
myItem.Selected = false;
}

1 comment:

Nick said...

Nice! post its really informative...

Software Product Development Services: - Ampere is a fast growing outsourced software product engineering company focused on helping software companies create innovative products for the next generation. Headquartered in India, Ampere has proven expertise and a deep domain understanding of technology to deliver software engineering services such as software product development (web/non web), product migration, software testing and custom application development maintenance to enterprises spread across the globe.