By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,357 Members | 982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,357 IT Pros & Developers. It's quick & easy.

Error Handling with response.write

P: n/a
Hello,
I have a separate Database class that handles any database work that all my
asp.net pages can use.

My problem is, many times I use try/catch to catch errors, and I want to
output these errors to the webpage with response.write().

Unfortunately Response.anything from within my class generate a "Response is
not declared" error. How can I get Response to work from within a class, or
is there a better way to handle errors in asp.net classes.

Thanks,
--Michael
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
michael,

well try not handling the exception at that point instead use a global error
handler something like application_error
which can check the type of exception and do a server.transfer
(response.redirect would clear the error) to appropraite page to show the
error
or if you like to display it with calling page.... try wrapping a try catch
block when you the the database object.
catch the exception from within your aspx page

hope this helps

hd

"Michael" <raterus@localhost> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
Hello,
I have a separate Database class that handles any database work that all my asp.net pages can use.

My problem is, many times I use try/catch to catch errors, and I want to
output these errors to the webpage with response.write().

Unfortunately Response.anything from within my class generate a "Response is not declared" error. How can I get Response to work from within a class, or is there a better way to handle errors in asp.net classes.

Thanks,
--Michael

Nov 18 '05 #2

P: n/a
Don't have the class attempt to provide any kind of user interface.
Instead, use a Try...Catch and in the Catch, throw a new exception that will
bubble up to the applicaiton that is calling the class. This applicaiton
hand then display details about the exception in its interface.
"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
michael,

well try not handling the exception at that point instead use a global error handler something like application_error
which can check the type of exception and do a server.transfer
(response.redirect would clear the error) to appropraite page to show the
error
or if you like to display it with calling page.... try wrapping a try catch block when you the the database object.
catch the exception from within your aspx page

hope this helps

hd

"Michael" <raterus@localhost> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
Hello,
I have a separate Database class that handles any database work that all my
asp.net pages can use.

My problem is, many times I use try/catch to catch errors, and I want to
output these errors to the webpage with response.write().

Unfortunately Response.anything from within my class generate a

"Response is
not declared" error. How can I get Response to work from within a
class, or
is there a better way to handle errors in asp.net classes.

Thanks,
--Michael


Nov 18 '05 #3

P: n/a
Another solution depends on how separate that class should
be from asp.net context. If it doesn't matter, then you
can simply pass Response to this class' constructor and
use it inside of it. Will work just fine.

Regards.

-----Original Message-----
Hello,
I have a separate Database class that handles any database work that all myasp.net pages can use.

My problem is, many times I use try/catch to catch errors, and I want tooutput these errors to the webpage with response.write().

Unfortunately Response.anything from within my class generate a "Response isnot declared" error. How can I get Response to work from within a class, oris there a better way to handle errors in asp.net classes.

Thanks,
--Michael
.

Nov 18 '05 #4

P: n/a
Try something like this. Theoratically this should work

Your code

try
{
Code .....
}
catch (Exception e)
{
WriteException ( e);
}

Another function ...

WriteException (Exception e)
{
if (HttpContext.Current!=nothing)
{
HttpContext.Current.Response.Write (ex.Message)
}
else
{
// Skip since we are not running from ASP.NET
}

}

Thanks
Shiplu

On Fri, 26 Dec 2003 11:42:40 -0500, Scott M. <s-***@BADSPAMsnet.net> wrote:
Don't have the class attempt to provide any kind of user interface.
Instead, use a Try...Catch and in the Catch, throw a new exception that
will
bubble up to the applicaiton that is calling the class. This applicaiton
hand then display details about the exception in its interface.
"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
michael,

well try not handling the exception at that point instead use a global

error
handler something like application_error
which can check the type of exception and do a server.transfer
(response.redirect would clear the error) to appropraite page to show
the
error
or if you like to display it with calling page.... try wrapping a try

catch
block when you the the database object.
catch the exception from within your aspx page

hope this helps

hd

"Michael" <raterus@localhost> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
> Hello,
> I have a separate Database class that handles any database work that

all
my
> asp.net pages can use.
>
> My problem is, many times I use try/catch to catch errors, and I want

to
> output these errors to the webpage with response.write().
>
> Unfortunately Response.anything from within my class generate a

"Response
is
> not declared" error. How can I get Response to work from within a

class,
or
> is there a better way to handle errors in asp.net classes.
>
> Thanks,
> --Michael
>
>




--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 18 '05 #5

P: n/a
> WriteException (Exception e)
{
if (HttpContext.Current!=nothing)
{
HttpContext.Current.Response.Write (ex.Message)
}
else
{
// Skip since we are not running from ASP.NET
}
This code, though well intentioned, will not work as intended because the
context object is always null outside of the page - as in a user class for
example. What is needed is a way to pass a reference of the context object
to the class library.

I posted this to another newsgroup. It applies here as well.

You can pass in the context object which houses the request and response
object for an application to your method. Your method will contain the logic
to manipulate the response and request objects. Consider:

private void somefunction(System.Web.HttpContext Stream)
{

Stream.Session["session_id"] = "value";

}
> Unfortunately Response.anything from within my class generate a

"Response
is
> not declared" error. How can I get Response to work from within a

class,
Armed with this general idea, you can adapt your class to take a reference
to the context object when it is instantiated. When you need to touch the
response object, you will touch it thru the reference provided in your
class. As always, be careful to properly release resources when they aren't
needed.

--
Regards,
Alvin Bruney
Got DotNet? Get it here
http://home.networkip.net/dotnet/tidbits/default.htm
"Shafqat Ahmed" <ad***@shiplu.com> wrote in message
news:op**************@news.microsoft.com... Try something like this. Theoratically this should work

Your code

try
{
Code .....
}
catch (Exception e)
{
WriteException ( e);
}

Another function ...

WriteException (Exception e)
{
if (HttpContext.Current!=nothing)
{
HttpContext.Current.Response.Write (ex.Message)
}
else
{
// Skip since we are not running from ASP.NET
}

}

Thanks
Shiplu

On Fri, 26 Dec 2003 11:42:40 -0500, Scott M. <s-***@BADSPAMsnet.net> wrote:
Don't have the class attempt to provide any kind of user interface.
Instead, use a Try...Catch and in the Catch, throw a new exception that
will
bubble up to the applicaiton that is calling the class. This

applicaiton hand then display details about the exception in its interface.
"Hermit Dave" <he************@CAPS.AND.DOTS.hotmail.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
michael,

well try not handling the exception at that point instead use a global

error
handler something like application_error
which can check the type of exception and do a server.transfer
(response.redirect would clear the error) to appropraite page to show
the
error
or if you like to display it with calling page.... try wrapping a try

catch
block when you the the database object.
catch the exception from within your aspx page

hope this helps

hd

"Michael" <raterus@localhost> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl...
> Hello,
> I have a separate Database class that handles any database work that
all
my
> asp.net pages can use.
>
> My problem is, many times I use try/catch to catch errors, and I want
to
> output these errors to the webpage with response.write().
>
> Unfortunately Response.anything from within my class generate a

"Response
is
> not declared" error. How can I get Response to work from within a

class,
or
> is there a better way to handle errors in asp.net classes.
>
> Thanks,
> --Michael
>
>



--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.