tags:

views:

29

answers:

3

I am getting the following exception from my ASP page during rendering:

Cannot have multiple items selected in a DropDownList.

at System.Web.UI.WebControls.DropDownList.VerifyMultiSelect() at System.Web.UI.WebControls.ListControl.RenderContents(HtmlTextWriter writer) at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Page.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

None of my code is in the stack trace, and my page has seven drop down lists on it. I am setting the selected rows on the drop down lists in my code based on data from several database tables as well as rules about default selections. So it is quite possible I have a bug where I set the selected item on one of my fields. But how can I tell which field is causing the problem?

In Global.asax I have

    void Application_Error(object sender, EventArgs e) 
    {
        // Code that runs when an unhandled error occurs
        Exception lastException = Server.GetLastError();
    }

Which I am able to set a breakpoint on but nothing in the exception seems to tell me which field was being rendered when the exception got thrown.

Any hints on where to find this information or debugging strategies that will help me find it?

+1  A: 

Launch exception dialog using Ctrl+Alt+E option and on "Common Language Runtime Exceptions" row select "Thrown" checkbox. Attach the worker process and it will directly take you to the line that is causing the problem.

Pradeep
This works nicely for exceptions thrown by my code, but here the exception is being thrown by the framework which (I assume) is compiled without debug support. The debugger does not break for me in this case.
verisimilidude
Another option would be to comment code for each drop down list one by one and then see what part of code is causing problem. It is time consuming but guess the only way if debugging is not helping.
Pradeep
A: 

What I had to do was check all the drop down controls in the pre-render to find which ones where causing the problem. Once I could find that I was able to zero in on which fields where causing the problem and found my bug. The bug was caused because setting the Selected property of a list item in a drop down does not unselect other items, but setting the DropDownList's SelectedItem or SelectedIndex property does unselect previously selected items.

Since this is my code I could stop on it with the debugger when it found two items selected. Using quickView you can examine the field ddl.SelectedIndicesInternal.Count, a protected field that will tell you directly (with the need for the loop) how many (and which ones, if you look into the array) elements are selected. Unfortunately this is a protected field and can't be checked by my code without getting around the protection level.

verisimilidude
You should put this into the question and not as an answer to the question.
Martin Hyldahl
A: 
verisimilidude
You should edit the question and post the code there instead of as an answer to the question.
Martin Hyldahl