Link to home
Start Free TrialLog in
Avatar of David Bach
David BachFlag for United States of America

asked on

ASP.NET, SQL Server - Do Error Messages Have Error Numbers?

Greetings:

In my ASP.NET app there are occasions whereby the yellow error screen is displayed on a web browser. I would like to intercept these exceptions sending myself an e-mail about the exception and providing a simple, non-technical explanation to the user instead of a yellow screen and a message the user most likely will not understand.

I'm hoping most error messages have a unique error number by product. Product is IIS, SQL Server, ASP.NET, etc. In this case checking for an error number, and not the contents of an error message, will simplify coding.

I hope I've stated my question intelligently.

Much thanks in advance,
David Bach
Avatar of John Gates, CISSP, CDPSE
John Gates, CISSP, CDPSE
Flag of United States of America image

Can you post an example of one of these errors?  Trying to visualize what you need.
use custom errors

https://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.85).aspx

<customErrors defaultRedirect="MyError.aspx"  mode="On">
</customErrors>

Open in new window


then on that page, handle error, log it, insert into db, send email etc... and show user friendly message...
Avatar of David Bach

ASKER

Greetings John:

One of our sites uses MS SQL as a data repository. When a query is successfully initiated against the database the results are stored in a session state variable for example 'SessionAdd("s_dsCatalog", dsCatalog). When a post back occurs the dsCatalog object is refreshed from the session variable.

A user clicks on a button to retrieve data from this database. The user gets his data. The user does not interact with the web server until after the session state timeout value is exceeded. The user then sees a message indicating "OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT" when attempting to retrieve more data from the database.

Is the message "OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT" only identifiable by the context of the message itself, or does this specific message have a message number to refer to it by? If so, how would I access the message number in order to create my own message for the user?

I'm also asking if all SQL, web server messages are identifiable by number and not just their message context.

I hope this doesn't muddy the water too much.


Much thanks,
David Bach
Can you post the portion of the code that renders the error?  That will help me post a code example to extract additional data.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi John:

I would like to post code to exemplify my question, however, this is a timeout whereby the web server session state time allowance for the user's session has expired. This is usually due to a web user not interacting with the web server for a period of time which exceeds the session state time allowance.

If you were to create an ASP.NET page as follows:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SessionTimeout.aspx.vb" Inherits="SessionTimeout" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
		<asp:Button ID="Button1" runat="server" Text="Display" /><br /><br />
		<asp:DataList ID="DataList1" runat="server"></asp:DataList>
    </div>
    </form>
</body>
</html>

Open in new window

With code of:
Imports System.Data

Partial Class SessionTimeout
	Inherits System.Web.UI.Page

	Dim ds As New DataSet
	Dim dt As DataTable
	Dim dr As DataRow
	Dim idColumn As DataColumn
	Dim nameColumn As DataColumn

	Private Sub SessionTimeout_Load(sender As Object, e As EventArgs) Handles Me.Load

		If Not IsPostBack Then
			LoadTable()
		End If

	End Sub

	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

		ds = Session.Item("s_ds")

		DataList1.DataSource = ds.Tables("dt")
		DataList1.DataBind()

	End Sub

	Protected Sub LoadTable()

		If Not IsPostBack Then
			dt = New DataTable()
			idColumn = New DataColumn("ID", Type.GetType("System.Int32"))
			nameColumn = New DataColumn("Name", Type.GetType("System.String"))

			dt.Columns.Add(idColumn)
			dt.Columns.Add(nameColumn)

			dr = dt.NewRow()
			dr("ID") = 1
			dr("Name") = "Name1"
			dt.Rows.Add(dr)

			dr = dt.NewRow()
			dr("ID") = 2
			dr("Name") = "Name2"
			dt.Rows.Add(dr)

			ds.Tables.Add(dt)

			Session.Add("s_ds", ds)

			Label1.Text = "Table loaded."
		End If

	End Sub

End Class

Open in new window

Set the session timeout on the web server for 1 minute then run the page. Wait 1 minute, click the button and the error will occur.

I hope this helps more.


Much thanks,
David Bach
HI Huseyin:

I will try looking at the HResult property within a Try statement.


Much thanks,
David Bach
Hi Huseyin:

Using a Try statement I did capture the decimal error code -2147467261. Translating this to hex and referring to only the low order 4 bytes I come up with 0x80004003. Looking this up on MSDN it says "pointer error" which makes complete sense to me since the session state was null by this time.

When I searched for "Object reference not set to an instance of an object." on the same MSDN page there was no match.

This is a step in the right direction for me. I would like to see what John might add before closing this question.


Much thanks,
David Bach
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi John and Huseyin:

I did not know I could have to Catch phrases within 1 Try - Or possibly I didn't know I knew.

In any case you have both helped me.

Thank you for your time, expertise and patience,
David Bach
Thank you both for your time, expertise and patience!
David Bach
Glad to help!  Happy programming :-)