[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to display in ASP.NET an error message caught in a catch block using the C# code-behind?

Posted on 2004-09-27
13
Medium Priority
?
436 Views
Last Modified: 2008-03-10
How to display in ASP.NET an error message caught in a catch block using the C# code-behind?
I would like to display the message in a something like MessageBox so that a user could close it.
I know that JavaScript alert( ) could be used to display client-side messages, but how to integrate the alert( ) into the C# code-behind?  And are there any other ways to do it?

Thanks for your help.
0
Comment
Question by:pavelmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12161148
in your catch block....

catch(Exception ex)
{
string strScript = "<script language=JavaScript>alert('" +
                       ex.Message + "')</script>";
      if (!(senderPage.IsStartupScriptRegistered(alertKey)))
      senderPage.RegisterStartupScript(alertKey, strScript);

}
0
 

Author Comment

by:pavelmed
ID: 12161260
What is "alertKey" ?
Thanks
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12161296
it is any name...

you can say Page.RedisterStartupScript("alertkeyname_any string",strScript);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:AerosSaga
ID: 12161347
I usually just create a label on the page, and set it to visible and then right the error message like so:

Try
            Me.lblAuthCode.Text = CStr(AuthNetTrans.AuthCode.ToString)
            Me.lblTransID.Text = CStr(AuthNetTrans.TransID.ToString)
        Catch ex As PaymentProcessing.AuthorizeNetException
            ''Double check this part
            Me.lblError.Text = "We are sorry there was an error of type" & ex.Message & " while processing your card."
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "UPDATE ORDERS SET ResponseReasonText = " & ex.Message.ToString & " Where SessionID = '" & Me.lblTrackingID.Text.ToString & "'"
            cmd.Connection = cnn
            cnn.Open()
            cmd.ExecuteNonQuery()
            cnn.Close()
            cmd.Dispose()
            cmd.Dispose()
        End Try
0
 

Author Comment

by:pavelmed
ID: 12161352
What should be a namespace for the senderPage?
Thanks
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12161366
Catch e As Exception
            Try
                Trans.Rollback()
                cmd.CommandText = "UPDATE Orders SET Processed = 'False' WHERE OrderTrackingID = " & Me.lblOrderTrackingID.Text
                cmd.ExecuteNonQuery()
                Me.lblError.Text = e.Message.ToString
            Catch ex As ByteFX.Data.MySqlClient.MySqlException
                If Not Trans.Connection Is Nothing Then
                    Me.lblError.Text = ex.Message.ToString
                End If
            End Try
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12161390
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12161406
Dim strScript As String = "<script language=JavaScript>"
  strScript += "alert(""" & strMessage & """);"
  strScript += "</script>"

  If (Not Page.IsStartupScriptRegistered("clientScript")) Then
     Page.RegisterStartupScript("clientScript", strScript)
  End If
0
 

Author Comment

by:pavelmed
ID: 12161483
I have "using System.Web.UI;" in my C# code, but the compiler displays an error: "The type or namespace name 'senderPage' could not be found (are you missing a using directive or an assembly reference?"
Thanks
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12161501
no don't use sender page..just say Page.Register....
0
 
LVL 15

Accepted Solution

by:
praneetha earned 2000 total points
ID: 12161505
catch(Exception ex)
{
string strScript = "<script language=JavaScript>alert('" +
                       ex.Message + "')</script>";
     if (!(Page.IsStartupScriptRegistered(alertKey)))
     Page.RegisterStartupScript(alertKey, strScript);

}
0
 

Author Comment

by:pavelmed
ID: 12162034
Thank you all.
I have accepted the answer from praneetha as it fits my goals the best.
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12162042
Thank you.

Good Luck
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question