pavelmed
asked on
How to display in ASP.NET an error message caught in a catch block using the C# code-behind?
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.
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.
ASKER
What is "alertKey" ?
Thanks
Thanks
it is any name...
you can say Page.RedisterStartupScript ("alertkey name_any string",strScript);
you can say Page.RedisterStartupScript
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.Authoriz eNetExcept ion
''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.ToSt ring & "'"
cmd.Connection = cnn
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
cmd.Dispose()
cmd.Dispose()
End Try
Try
Me.lblAuthCode.Text = CStr(AuthNetTrans.AuthCode
Me.lblTransID.Text = CStr(AuthNetTrans.TransID.
Catch ex As PaymentProcessing.Authoriz
''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.ToSt
cmd.Connection = cnn
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
cmd.Dispose()
cmd.Dispose()
End Try
ASKER
What should be a namespace for the senderPage?
Thanks
Thanks
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.My SqlExcepti on
If Not Trans.Connection Is Nothing Then
Me.lblError.Text = ex.Message.ToString
End If
End Try
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.My
If Not Trans.Connection Is Nothing Then
Me.lblError.Text = ex.Message.ToString
End If
End Try
Dim strScript As String = "<script language=JavaScript>"
strScript += "alert(""" & strMessage & """);"
strScript += "</script>"
If (Not Page.IsStartupScriptRegist ered("clie ntScript") ) Then
Page.RegisterStartupScript ("clientSc ript", strScript)
End If
strScript += "alert(""" & strMessage & """);"
strScript += "</script>"
If (Not Page.IsStartupScriptRegist
Page.RegisterStartupScript
End If
ASKER
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
Thanks
no don't use sender page..just say Page.Register....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all.
I have accepted the answer from praneetha as it fits my goals the best.
I have accepted the answer from praneetha as it fits my goals the best.
Thank you.
Good Luck
Good Luck
catch(Exception ex)
{
string strScript = "<script language=JavaScript>alert(
ex.Message + "')</script>";
if (!(senderPage.IsStartupScr
senderPage.RegisterStartup
}