Global.asax file

Discussion in 'ASP .Net' started by paul downing via DotNetMonster.com, Dec 15, 2004.

  1. global file email send

    --------------------------------------------------------------------------------

    guys, i'm developing an application and want to send an email with an error if it occurs to the administrator... i tried the following below but can't get it working.

    I did have it working a while ago...but have had no success with this.... any ideas?

    heres the code (simplistic) from the global.asax file:

    <%@ Import Namespace="System.Web.Mail" %>
    <%@ Import Namespace="System.text" %>
    <%@ Import Namespace="System.web" %>
    <%@ Import Namespace="System.web.sessionstate" %>
    <%@ Import Namespace="System.Web.Mail" %>
    <%@ Import Namespace="System.Text" %>
    <script language="VB" runat="server">

    Public Class Global
    Inherits System.Web.HttpApplication

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)


    Dim mail As New MailMessage()
    Dim ErrorMessage = "The error description is as follows : " & Server.GetLastError.ToString
    mail.To = ""
    mail.Subject = "Error in the Site"
    mail.Priority = MailPriority.High
    mail.BodyFormat = MailFormat.Text
    mail.Body = ErrorMessage
    SmtpMail.Send(mail)

    End Sub

    End Class



    i have even tried a simple reponse.write in the application_error and can't seem to make it trigger! Can someone point me in the right direction?

    thanks

    --
    Message posted via http://www.dotnetmonster.com
     
    paul downing via DotNetMonster.com, Dec 15, 2004
    #1
    1. Advertising

  2. hai paul,
    Just Try giving SmtpServerName/IP it may help u

    System.Web.Mail.MailMessage mailer = new System.Web.Mail.MailMessage();
    mailer.To = ToAddress;
    mailer.From = FromAddress;
    mailer.Subject = Subject;
    mailer.Body = Body;
    SmtpMail.SmtpServer = SmtpServerName/IP
    SmtpMail.Send(mailer);

    Good Luck
    Mahi

    "paul downing via DotNetMonster.com" wrote:

    > global file email send
    >
    > --------------------------------------------------------------------------------
    >
    > guys, i'm developing an application and want to send an email with an error if it occurs to the administrator... i tried the following below but can't get it working.
    >
    > I did have it working a while ago...but have had no success with this.... any ideas?
    >
    > heres the code (simplistic) from the global.asax file:
    >
    > <%@ Import Namespace="System.Web.Mail" %>
    > <%@ Import Namespace="System.text" %>
    > <%@ Import Namespace="System.web" %>
    > <%@ Import Namespace="System.web.sessionstate" %>
    > <%@ Import Namespace="System.Web.Mail" %>
    > <%@ Import Namespace="System.Text" %>
    > <script language="VB" runat="server">
    >
    > Public Class Global
    > Inherits System.Web.HttpApplication
    >
    > Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    >
    >
    > Dim mail As New MailMessage()
    > Dim ErrorMessage = "The error description is as follows : " & Server.GetLastError.ToString
    > mail.To = ""
    > mail.Subject = "Error in the Site"
    > mail.Priority = MailPriority.High
    > mail.BodyFormat = MailFormat.Text
    > mail.Body = ErrorMessage
    > SmtpMail.Send(mail)
    >
    > End Sub
    >
    > End Class
    >
    >
    >
    > i have even tried a simple reponse.write in the application_error and can't seem to make it trigger! Can someone point me in the right direction?
    >
    > thanks
    >
    > --
    > Message posted via http://www.dotnetmonster.com
    >
     
    =?Utf-8?B?TWFoaXBhbCBSZWRkeQ==?=, Dec 15, 2004
    #2
    1. Advertising

  3. Paul,

    Check out sample code for this at
    http://support.microsoft.com/default.aspx?scid=kb;en-us;308132

    "How to Use the Application_Error Event"

    You could send mail, instead of writing
    to the log as the example does.



    Juan T. Llibre
    ===========
    "paul downing via DotNetMonster.com" <> wrote in
    message news:...
    > global file email send
    >
    > --------------------------------------------------------------------------------
    >
    > guys, i'm developing an application and want to send an email with an
    > error if it occurs to the administrator... i tried the following below but
    > can't get it working.
    >
    > I did have it working a while ago...but have had no success with this....
    > any ideas?
    >
    > heres the code (simplistic) from the global.asax file:
    >
    > <%@ Import Namespace="System.Web.Mail" %>
    > <%@ Import Namespace="System.text" %>
    > <%@ Import Namespace="System.web" %>
    > <%@ Import Namespace="System.web.sessionstate" %>
    > <%@ Import Namespace="System.Web.Mail" %>
    > <%@ Import Namespace="System.Text" %>
    > <script language="VB" runat="server">
    >
    > Public Class Global
    > Inherits System.Web.HttpApplication
    >
    > Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    >
    >
    > Dim mail As New MailMessage()
    > Dim ErrorMessage = "The error description is as follows : " &
    > Server.GetLastError.ToString
    > mail.To = ""
    > mail.Subject = "Error in the Site"
    > mail.Priority = MailPriority.High
    > mail.BodyFormat = MailFormat.Text
    > mail.Body = ErrorMessage
    > SmtpMail.Send(mail)
    >
    > End Sub
    >
    > End Class
    >
    >
    >
    > i have even tried a simple reponse.write in the application_error and
    > can't seem to make it trigger! Can someone point me in the right
    > direction?
    >
    > thanks
    >
    > --
    > Message posted via http://www.dotnetmonster.com
     
    Juan T. Llibre [MVP], Dec 15, 2004
    #3
  4. thanks for the help guys....


    i've now tried just this in my global.asax file and i just can't seem to trigger anything


    <script language="VB">



    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    response.write("hello")
    End Sub


    </script>



    any ideas?

    --
    Message posted via http://www.dotnetmonster.com
     
    paul downing via DotNetMonster.com, Dec 15, 2004
    #4
  5. Paul,

    You cannot use Response.Write in global.asax

    Where (in what page) would
    the Response object write "hello" to ?



    Juan T. Llibre
    ===========
    "paul downing via DotNetMonster.com" <> wrote in
    message news:...
    > thanks for the help guys....
    >
    > i've now tried just this in my global.asax file and i just can't seem to
    > trigger anything
    >
    > <script language="VB">
    > Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    > response.write("hello")
    > End Sub
    > </script>
    >
    > any ideas?
     
    Juan T. Llibre [MVP], Dec 15, 2004
    #5
  6. paul downing via DotNetMonster.com

    pete Guest

    can you post the code that causes the error (or add one). if the originating
    error is contained within a try / catch statement. you may need to rethrow
    it for the application error event to fire

    Cheers, pete

    "paul downing via DotNetMonster.com" <> wrote in
    message news:...
    > thanks for the help guys....
    >
    >
    > i've now tried just this in my global.asax file and i just can't seem to

    trigger anything
    >
    >
    > <script language="VB">
    >
    >
    >
    > Sub Application_Error(ByVal sender As Object, ByVal e As

    EventArgs)
    > response.write("hello")
    > End Sub
    >
    >
    > </script>
    >
    >
    >
    > any ideas?
    >
    > --
    > Message posted via http://www.dotnetmonster.com
     
    pete, Dec 15, 2004
    #6
  7. Referencing the Session, Request, Response or Page
    objects within application scope generates an error.

    Application_Error only fires when an unhandled
    exception is thrown, so you can't just Response.write
    from it

    You could *define* what happens when an
    unhandled exception occurs, but you can't
    do anything else within the Application_Error event.

    Errors that are handled with error trapping
    code (such as Try/Catch blocks) will *not*
    cause the Application_Error event to fire.

    It's impossible for response.write("hello")
    to occur.within Application_Error.




    Juan T. Llibre
    ===========
    "pete" <> wrote in message
    news:...
    > can you post the code that causes the error (or add one). if the
    > originating error is contained within a try / catch statement.
    > you may need to rethrow it for the application error event to fire
    >
    > Cheers, pete
    >
    > "paul downing via DotNetMonster.com" <> wrote in
    > message news:...
    >> thanks for the help guys....
    >>
    >>
    >> i've now tried just this in my global.asax file and i just can't seem to

    > trigger anything
    >>
    >>
    >> <script language="VB">
    >>
    >>
    >>
    >> Sub Application_Error(ByVal sender As Object, ByVal e As

    > EventArgs)
    >> response.write("hello")
    >> End Sub
    >>
    >>
    >> </script>
    >>
    >>
    >>
    >> any ideas?
    >>
    >> --
    >> Message posted via http://www.dotnetmonster.com

    >
    >
     
    Juan T. Llibre [MVP], Dec 15, 2004
    #7
  8. paul downing via DotNetMonster.com, Dec 16, 2004
    #8
  9. right...

    heres my page to generate an error when i click on the button..

    <%@ Page Language="VB" %>
    <script runat="server">

    ' Insert page code here
    '

    Sub Button1_Click(sender As Object, e As EventArgs)

    throw(new ArgumentNullException())

    End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
    <!-- Insert content here -->
    </form>
    </body>
    </html>
    ------------------------------------------------------------------>


    and heres my global.asax file code....


    <script language="VB">



    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    response.redirect("error.aspx")
    End Sub


    </script>

    -----------------------------------------------
    am i doing something wrong?

    --
    Message posted via http://www.dotnetmonster.com
     
    paul downing via DotNetMonster.com, Dec 16, 2004
    #9
  10. paul downing via DotNetMonster.com, Dec 16, 2004
    #10
  11. Hi, Paul.

    If all you want to do is redirect the user to an error page,
    you're far better off using a custom error page.

    See
    http://msdn.microsoft.com/library/d...us/cpgenref/html/gngrfCustomerrorsSection.asp
    and
    http://msdn.microsoft.com/asp.net/a...l=/library/en-us/dnaspp/html/customerrors.asp

    There's a great source code sampler at
    http://download.microsoft.com/downl...4057-af24-9e1a5b34b3dc/CustomErrorsSample.msi
    ( 341KB )

    There's a very simple walkthrough for custom error reporting at
    http://www.c-sharpcorner.com/Code/2002/May/CustomErrorHandlingASPNEt.asp
    It explains the basics without much ado.





    Juan T. Llibre
    ===========
    "paul downing via DotNetMonster.com" <> wrote in
    message news:...
    > right...
    >
    > heres my page to generate an error when i click on the button..
    >
    > <%@ Page Language="VB" %>
    > <script runat="server">
    >
    > ' Insert page code here
    > '
    >
    > Sub Button1_Click(sender As Object, e As EventArgs)
    >
    > throw(new ArgumentNullException())
    >
    > End Sub
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form runat="server">
    > <asp:Button id="Button1" onclick="Button1_Click" runat="server"
    > Text="Button"></asp:Button>
    > <!-- Insert content here -->
    > </form>
    > </body>
    > </html>
    > ------------------------------------------------------------------>
    >
    >
    > and heres my global.asax file code....
    >
    >
    > <script language="VB">
    >
    >
    >
    > Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    > response.redirect("error.aspx")
    > End Sub
    >
    >
    > </script>
    >
    > -----------------------------------------------
    > am i doing something wrong?
    >
    > --
    > Message posted via http://www.dotnetmonster.com
     
    Juan T. Llibre [MVP], Dec 16, 2004
    #11
  12. paul downing via DotNetMonster.com, Dec 16, 2004
    #12
  13. i have managed to get my error email working.

    But I can't seem to trigger my global.asax error function,no matter what I do!

    The web config file is fine and works by redirecting on an error, but I need to be able to send the email from the global.asax file but it just doesn't seem to trigger at all.

    Any ideas?

    --
    Message posted via http://www.dotnetmonster.com
     
    paul downing via DotNetMonster.com, Dec 16, 2004
    #13
  14. Have you got a Try Catch block in the page from where the mail/error
    originates?

    with regards,


    J.V.Ravichandran
    - http://www.geocities.com/
    jvravichandran
    - http://www.411asp.net/func/search?
    qry=Ravichandran+J.V.&cob=aspnetpro
    - http://www.southasianoutlook.com
    - http://www.MSDNAA.Net
    - http://www.csharphelp.com
    - http://www.poetry.com/Publications/
    display.asp?ID=P3966388&BN=999&PN=2
    - Or, just search on "J.V.Ravichandran"
    at http://www.Google.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Ravichandran J.V., Dec 18, 2004
    #14
  15. thanks for the help guys,

    the problem was a simple one

    my global file was not in the root folder... i thought it was but it wasn't ... the folder i was using need to bee set to an application is IIS on the server.

    Thanks again

    --
    Message posted via http://www.dotnetmonster.com
     
    paul downing via DotNetMonster.com, Dec 20, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    793
    Alvin Bruney
    Jul 29, 2003
  2. Daniel Bass
    Replies:
    2
    Views:
    6,887
    Daniel Bass
    Dec 31, 2003
  3. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,834
  4. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    846
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  5. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,172
    Mark Rae
    May 23, 2006
Loading...

Share This Page