Try / Catch Exception on ASPX During Page_Load or DataBind - c#

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.

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.

Related

Conflict between Sharepoint List Name and Document Library Name

I my share point site I have one List named "People List" and one Wiki Page Library named "People". I have created one visual webpart to display data from People list. I have created webpart property to read listname.The webpart property name for my visual webpart is "People". When I check whether list exists in my site using the following function it returns true:-
public static bool isListExists(SPWeb spWeb, string listName)
{
return spWeb.Lists.TryGetList(listName) != null;
}
But there is no List named "People" exists in my site. Only wiki page library named "People" exists. When I added my webpart in "MySite/People/default.aspx" page I got error.
Column 'Title' does not exist. It may have been deleted by another user.
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.ArgumentException: Column 'Title' does not exist. It may have been deleted by another user.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentException: Column 'Title' does not exist. It may have been deleted by another user.]
Microsoft.SharePoint.SPFieldCollection.GetField(String strName, Boolean bThrowException) +26422968
Microsoft.SharePoint.SPListItem.GetValue(String strName, Boolean bThrowException) +47
Microsoft.SharePoint.SPListItem.get_Item(String fieldName) +12
CL.DanceStudio.Portal.CLDance_Instructor.<>c__DisplayClass3.<Select>b__1() +1155
Microsoft.SharePoint.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2() +729
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27491206
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +27194345
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +93
CL.DanceStudio.Portal.CLDance_Instructor.CLDance_InstructorUserControl.Select() +291
CL.DanceStudio.Portal.CLDance_Instructor.CLDance_InstructorUserControl.Page_Load(Object sender, EventArgs e) +52
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.Control.AddedControl(Control control, Int32 index) +350
CL.DanceStudio.Portal.CLDance_Instructor.CLDance_Instructor.CreateChildControls() +194
System.Web.UI.Control.EnsureChildControls() +146
System.Web.UI.Control.PreRenderRecursiveInternal() +61
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Control.PreRenderRecursiveInternal() +224
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394
I think it is due to name conflict. Please provide me a solution.

Debugging compiled function in ASP .NET

I have a website I believe to be in ASP .NET, which is throwing an exception System.NullReferenceException: Object reference not set to an instance of an object.. Stack trace shows as follows:
[NullReferenceException: Object reference not set to an instance of an object.]
SomeScript.getConnection() +32
SomeScript.buildGridData() +299
SomeScript.Page_Load(Object sender, EventArgs e) +92
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Control.LoadRecursive() +163
System.Web.UI.Control.LoadRecursive() +163
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2603
Same error is occurring for basically all pages of the site which request data, with just the SomeScript in above stack trace alternating for the loading script.
Environmental changes have only been in changing password for a remote SMTP server the site uses for mailing out, but site was continuing to load fine after that change was made.
My feeling is that these errors with the getConnection() method started occurring after a user tried to use said SMTP mailing function within the website and received an error (I was not able to capture the error user received).
I'm a bit lost with debugging any further from here as the getConnection() method seems to be within compiled DLLs and not accessible by me.
What is the next step in trying to remove the error?
Firstly, it's unlikely that an SMTP password change has caused the issue from your description of it. But if that password change was made in the Web.config file, I'd say it's possible it was made less carefully than it should have been, and in fact a database connection-related section in there has been inadvertently modified and is no longer valid.
If you want to be able to debug into the DLL, you can disable the option to debug "Just My Code" in Visual Studio (something like Tools/Options/Debugging) - you should then be able to step down into these DLLs and see where the issue lies. Hopefully it won't require any code changes, and you can just fix up the configuration file.

Upgrade with SQL

I am converting a website over to ASP.NET 2.0, which supports .NET Framework versions 2.0 – 3.5. I am getting the following error message on a particular tab that is connected to a SQL DB. I am stuck at where this change needs to occur as I did not build this particular section of the site. Here is the error message and the Code. Any Help would be greatly appreciated.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MagicAjax.AjaxCallHelper.GetPageHiddenDictionary(Page page, String fieldName) +37
MagicAjax.AjaxCallHelper.HandleClientScriptBlocks(Page page, Boolean
reflectToClient) +17
MagicAjax.AjaxCallHelper.Page_PreRender(Object sender, EventArgs e) +238
System.Web.UI.Control.OnPreRender(EventArgs e) +8996626
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +2496

.NET Unhandled Exception from MasterPage

I'm copying and pasting my application from one server (works fine) to another server (it doesn't work fine).
I get this on the new server, even though the code hasn't changed:
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MasterPagePS_Index.Page_Load(Object sender, EventArgs e) +117
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Are you accessing a database, file, or any external resources? Sounds like you have a configuration difference, check your web.config.
You don't use relative path in oder to access your ressource (File, page, style, assembly , etc...)
so ASP.Net don't find your file, throw null exception.
The error you provided doesn't give much information. I suggest that you perform a remote debugging and check which line is causing the error.
Or you can add the Global.asax file and catch the error in Application_Error and email the error to yourself.
Dim ex As Exception = Server.GetLastError()
Dim hue As System.Web.HttpUnhandledException = New System.Web.HttpUnhandledException(ex.Message, ex)
Dim html As String = hue.GetHtmlErrorMessage()
' TODO: Send the html string. This will send you the formatted html of the exception including the source of the error and stacktrace.

What does the number accompanying the line number in StackTrace mean?

I am receiving an error in the hosted app and am trying to understand why the error is happening.
For this i look at the stack trace on the error page, which is as follows:
[
NullReferenceException: Object reference not set to an instance of an object.]
MBANinja.demo.DemoTestPage.Page_Load(Object sender, EventArgs e) +696
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +41
System.Web.UI.Control.OnLoad(EventArgs e) +131
System.Web.UI.Control.LoadRecursive() +65
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427
Now I always thought the number accompanying the method call is the line number where the error was encountered. But that particular page has code (DemoTestPage)e only till 220's. So wht does +696 mean?
You get line numbers only if debug information (the pdb file) is available, otherwise you get an offset in the current method.

Categories

Resources