Share via


How To Show Try Catch Error Message

Question

Thursday, June 23, 2011 2:55 AM

Hi all..

i want Show try catch error message in a message box i use this coding for it....

 Protected Sub btnADD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnADD.Click
        Dim fno, Sno, Total As Integer
        Try
            fno = txtfno.Text
            Sno = txtSno.Text
            Total = fno + Sno
            txtTotal.Text = Total
            Response.Write("<script language='javascript'>alert('Record added Successfully.')</script>")

        Catch ex As Exception
            Response.Write("<script language='javascript'>alert(" & Err.Description & ")</script>")
           
        End Try
    End Sub

it only shows if result correct othewise won't...error message won't show..


Thanks & Regards
S.Priyan 

All replies (20)

Wednesday, June 29, 2011 6:56 AM âś…Answered

Finally I Got The Answer..

 Response.Write("<script language=""javascript"">")
            If ex.InnerException Is Nothing Then

                Response.Write("alert(""" + ex.Message.ToString() + """);")
            Else
                Response.Write("alert(""" + ex.InnerException.Message.ToString() + """);")
            End If
            Response.Write("</script>")


Thanks Those Who Intrest to response this..

Thanks & Regards
S.PRiyan

Thursday, June 23, 2011 3:01 AM

Response.Write("<script language='javascript'>alert(" & ex.Message & ")</script>")

Thursday, June 23, 2011 3:02 AM

you need to show in VB.Net

Try

   ''' you code here
Catch ex As Exception
   MessageBox.Show(ex.Message)
 End Try
a complete solution

http://www.dotnetperls.com/messagebox-show-vbnet
http://weblogs.asp.net/bleroy/archive/2005/12/01/asp-net-alerts-how-to-display-message-boxes-from-server-side-code.aspx

Thursday, June 23, 2011 3:22 AM

you need to show in VB.Net

Try

   ''' you code here
Catch ex As Exception
   MessageBox.Show(ex.Message)
 End Try
a complete solution

http://www.dotnetperls.com/messagebox-show-vbnet
http://weblogs.asp.net/bleroy/archive/2005/12/01/asp-net-alerts-how-to-display-message-boxes-from-server-side-code.aspx

Its a webapplication, MessageBox.Show won't work there


Thursday, June 23, 2011 3:27 AM

hi sir thanks For ur Reply,

  but again message don't show(Response.Write("<script language='javascript'>alert(" & ex.Message & ")</script>"))....please help me for this...

  thanks & Regards

ShunmugaPriyan.M


Thursday, June 23, 2011 3:28 AM

Oh, its missing the ' characters, try this:

Response.Write("<script language='javascript'>alert('" & ex.Message & "')</script>")

Thursday, June 23, 2011 4:47 AM

Thanks For Your Reply Sir,

                      It also don't work...Please Help For This.....

Thanks & Regards

ShunmugaPriyan.M


Thursday, June 23, 2011 4:53 AM

What happens? Do you get an errormessage or javascript error?


Thursday, June 23, 2011 4:55 AM

nothing Happent Just The Page Reloaded....please Help Me Boss........


Thursday, June 23, 2011 4:58 AM

I guess that you get some kind of javascript error on the page.

Can you alert just some text and see if you really end up in the catch block at all?


Thursday, June 23, 2011 5:19 AM

Hi Sir Thanks For Your Reply,

          i run the program via break error correctly caught but the message won't show..........Please Help Me For This

Thanks & Regard

Shunmugapriyan.m


Thursday, June 23, 2011 5:52 AM

Can you check your browser if you get any javascript errors?

also, try wrapping ex.Message like this:

HttpUtility.HtmlEncode(ex.Message)

 

if could be the case that the error contains a ' character and that will mess up the javascript


Thursday, June 23, 2011 7:02 AM

thanks FOr Reply Sir,

    it also Give Same Response.....there no message box shown in output...page reload again.please Help Me For This....

Thanks & Regards

S.Priyan


Thursday, June 23, 2011 7:10 AM

You need to check your browser for javascript errors. What browser and version are you using?


Thursday, June 23, 2011 7:17 AM

Insted of showing javascript alert on Successfull save, why dont you just show it in Label.

'If record saved then
InformationLabel.Text="Record added Successfully."


'If error then
InformationLabel.Text=ex.Message

Thursday, June 23, 2011 7:38 AM

Hi Boss,

    Thanks For Your Reply..........

your answer ok boss but i want show it in a message box...it's my need ........

Thanks & Regards

ShunmugaPriyan


Thursday, June 23, 2011 8:07 AM

Are you getting any javascript error at the left hand bottom of your browser, if so then please post it


Thursday, June 23, 2011 8:08 AM

So, what browser and version are you using?


Saturday, June 25, 2011 1:50 AM

No JavaScript Error Showing on that Page...

I used IE8 Browser boss

Thanks & regards

S.Priyan


Monday, June 27, 2011 12:49 AM

It must be a javascript error if you are entering the catch block