Reputation: 117
I am currently trying to help a client limp along with a very old .NET application written with C# and aspx. .NET and C# are not my primary, 2nd or even 3rd technologies I typically work with so my knowledge of them is rather limited.
The big problem is that we do not have the source code for the C# side of the application and the only thing we can look at or even edit is the aspx files.
We have a page that is giving us the following error:
Sequence contains more than one matching element
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains more than one matching element
Source Error:
Line 54: <asp:DataList ID="dlChoice" runat="server" DataSource='<%# GetChoices(Convert.ToInt32(Eval("QuestionId"))) %>' RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="Table" EnableViewState="false">
Line 55: <ItemTemplate>
Line 56: <span id='s<%# Eval("ChoiceId") %>' class='<%# GetChoiceStyle(Convert.ToInt32(Eval("ChoiceId"))) %>'><%# GetTextBoxes(Convert.ToInt32(Eval("QuestionId")), Convert.ToInt32(Eval("ChoiceId")),Convert.ToString(Eval("Points"))) %> <%# ReplaceToken(Eval("ChoiceText").ToString()) %></span>
Line 57: </ItemTemplate>
Line 58: </asp:DataList>
Source File: e:\inetpub\webapp\ScoringSheet.aspx Line: 56
Stack Trace:
[InvalidOperationException: Sequence contains more than one matching element]
System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source, Func`2 predicate) +1304639
lambda_method(ExecutionScope ) +97
System.Linq.EnumerableExecutor`1.Execute() +244
System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +154
System.Linq.Queryable.SingleOrDefault(IQueryable`1 source, Expression`1 predicate) +422
CPAT.ScoringSheet.GetChoiceStyle(Int32 choiceId) +298
ASP.scoringsheet_aspx.__DataBind__control11(Object sender, EventArgs e) in e:\inetpub\webapp\ScoringSheet.aspx:56
System.Web.UI.Control.OnDataBinding(EventArgs e) +132
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +170
System.Web.UI.Control.DataBindChildren() +11045679
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +175
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +734
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +89
System.Web.UI.Control.DataBindChildren() +11045679
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +674
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +68
System.Web.UI.Control.DataBindChildren() +11045679
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +924
System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +838
System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +46
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +147
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
System.Web.UI.WebControls.ListView.PerformSelect() +74
CPAT.ScoringSheet.Page_Load(Object sender, EventArgs e) +3500
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
System.Web.UI.Control.OnLoad(EventArgs e) +132
System.Web.UI.Control.LoadRecursive() +66
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428
Version Information: Microsoft .NET Framework Version:2.0.50727.5420; ASP.NET Version:2.0.50727.5420
My question: how can I add enough error handling to show a nice little error message where the offending tag is located but allow the rest of the page to render? This would allow me to see exactly what part of the data set is having problems. I suspect there is something amiss in the database (duplicate record, bad data, etc.) but I can't find or see the query that is running nor what data is breaking the page.
I tried adding various try / catch blocks but I suspect these data binds are run separately somehow and the try / catch I added are just ignored and the page still errors.
Upvotes: 0
Views: 1178
Reputation: 39258
The issue seems to be happening inside the method CPAT.ScoringSheet.GetChoiceStyle(Int32 choiceId)
Specifically there seems to be a SingleOrDefault call in there on the linq query that will fail if there is more than one element for the given input.
If this is not a valid condition I would check the data and fix it in the data. Otherwise you will have to change the code inside said method to allow more elements.
Upvotes: 1