Click to See Complete Forum and Search --> : class file


Huzaifa
April 9th, 2003, 03:41 AM
hi

i have a class file having a common error trapping function
whenever any error in the project occurs like session expiry etc.
it should display the error or goto login page depending on the type of error.

error i am getting is
System.Web.HttpException: Response is not available in this context.

also can u help me in finding out the error number

thanks
huzaifa



check out the code


common.cs file
--------------------
using System;
using System.Text;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace XYZ
{
public class common:System.Web.UI.Page
{
public common()
{

}
public void DispError(string errormsg,Label lblMessage)
{
if(errormsg.Equals("Object reference not set to an instance of an object.") || errormsg.Equals("Fill: SelectCommand.Connection property has not been initialized."))
{
Response.Write ("script window.parent.location.href='../login.aspx' /script ");
lblMessage.Text=errormsg;
}
else
{
lblMessage.Text=errormsg;
}
}
}
}


view.aspx.cs file
------------------
try{
connection code and database handling
..........

}
catch(Exception ex)
{
common cs=new common();
cs.DispError(ex.Message,lblMessage);
}

V. Lorenzo
April 9th, 2003, 12:20 PM
Hi:

First :
-----
You don't need to check for the values of the error messages to know the kind of error you'r dealing with. Instead of...

if (errormsg.Equals("Object reference not set to an instance of an object.") )
...etc.

...use an exception variable and check for it's type like :

if ( myException is NullReferenceException )
...etc.

Second :
---------
You may not simply create an instance of a Page derived class for showing an error message. The Page object is created by the web server framework in response to an HTTP request, so you don't do that by yourself.

- If you simply want to redirect the client to an existing web page, do the Response.redirect() stuff right there where you process the error (in the original page or control that detects the error).

- Or... add the following lines to the web.config file:

<configuration>
<system.web>
<customError mode="On" defaultRedirect="your_error_handling_page.aspx">
<error statusCode="404" redirect="NotFound.aspx" />
.......and so forth, for each error code....
</customError>
</system.web>
</configuration>

...and in the error handling page you may do what ever you need for error handling.

- Another error processing method is trapping the error in the Global.asax. Add the error handling code in the Application_Error event handling method. The Context.Server.GetLastError() method gives you access to the error information. After done, call the Context.Server.ClearError() method.

VLorz