VB.NET Custom Errors Messages - asp.net

I like to user custom errors in my site.
The Idea is to trap errors and display friendly meaningful messages to users.
The real error details (line number, Page ...) will sent to me by e-mail.
I am programming in VB.NET.
Until now every code that i found about this say's that i must use the global.asax for it.
I do not like to use global.asax, I just like to make an error page that will sent me a -email with all the error details.
Until now, i go to the web.config file and insert this line:
<customErrors defaultRedirect="test.aspx" mode="On"></customErrors>
test.aspx looks like this:
Sub Page_Error(sender as Object, e as EventArgs)
Dim ctxOBJ As HttpContext
Dim exceptionOBJ As Exception
Dim errorInfoTXT As String
ctxOBJ = HttpContext.Current()
exceptionOBJ = ctxOBJ.Server.GetLastError()
errorInfoTXT = "Offending URL: " & ctxOBJ.Request.Url.ToString() & _
"Source: " & exceptionOBJ.Source.ToString() & _
"Message: " & exceptionOBJ.Message.ToString() & _
"Stack trace: " & exceptionOBJ.StackTrace.ToString() & _
"Target Site: " & exceptionOBJ.TargetSite.ToString()
ctxOBJ.Response.Write (errorInfoTXT)
ctxOBJ.Server.ClearError ()
End Sub
When here is an error in some page its redirect me to test.aspx but not showing any error.
Thanks,
Roy Shoa.

You just need to change this to
Sub Page_Load(sender as Object, e as EventArgs)
It's not the error page that's erroring, so its error event never fires.

Related

JavaScript runtime error: 'RefreshImageVerifier' is undefined

I have a image verifier in oUpWizStep2 update panel of wizard steps 2. I wan to refresh the image through Page_PreRender but i get JavaScript runtime error: 'RefreshImageVerifier' is undefined. It works fine in others page without wizard steps.
my code below
Private Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
If Wizard1.ActiveStepIndex = 1 Then
Dim oUp As UpdatePanel = TryCast(Wizard1.FindControl("oUpWizStep2"), UpdatePanel)
Dim imgVerifier As ImageVerifier = CType(Wizard1.FindControl("ImgVerifierS2"), ImageVerifier)
' ## Refresh the verification code on call back.
ScriptManager.RegisterStartupScript(oUpWizStep2, oUpWizStep2.GetType, "scriptname", "RefreshImageVerifier('" & imgVerifier.ClientID & "','ImageVerifier.axd?uid=" & imgVerifier.MyUniqueID & "');", True)
End If
End Sub

LinkButton postbackurl Error in asp.net

I want to send a Message by clicking on a LinkButton through yahoo messenger in Asp.net. Some times It works and sometimes I get this errors: Windows can not access the spesified device, path or file. You may not have appropriate permission to access this item and also Error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 400 This is my code in VB, What is wrong with it?
Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
Dim idname As String = "YMID"
Dim message As String = "Hello, This is my YM message"
Dim sendim As String = String.Format("ymsgr:SendIM?{0}&m={1}", idname, message)
LinkButton2.PostBackUrl = (sendim)
End Sub

Message After Redirecting VB.Net

I redirect the user to the same page but I would like for a popup message stating the error. I cannot use messagebox because it does not work in the app. I tried Response.Write did not work. I have put a label with the error message but I do not know how to make it appear when the page reloads because of the error.
Could I use page_load, and IsPostBack? If so, how can I tell that it is reloading because of the catch error? Or if you know a better way...
Idea I have when error happens about the page_load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//This could be in IsPostBack if needed.
If (the catch error happen) then
Show Label next to the button.
EndIf
If Page.IsPostBack Then
End If
My code to redirect the user when error happens:
ElseIf e.CommandName = "Remove" Then
Try
Dim dto As PDto = MyPage.DelegateP.GetPDtoByPrimaryKey(Me.OPId)
If dto.Id > 0 Then
dto.LUser = MyPage.LUser
MyPage.DelegateP.DeletePDto(dto)
End If
Me.PanelOtherPForm.Style("display") = "none"
Me.FormViewDetails.Style("display") = "none"
Me.CurrentOPId = 0
Me.Repeater1.DataBind()
BubbleOPChanged()
Catch ex As Exception
Response.Redirect(String.Format("/Default.aspx?i={0}&c={1}", MyPage.DtoPage.PageID, Me.CId))
End Try
You could attach an error code to the URL as a parameter when you redirect.
Then on page load, check if the parameter is there and deal with it as you see fit.
Yes, MsgBox won't work when you publish your app to the server but you can make a pop up message with client side script. In this example, I force an error to happen and show you the error message in a pop up message that should run fine on your server.
Try
'Force an error to happen for testing
Dim test As Integer
test = "test"
Catch ex As Exception
Dim errorText As String
errorText = Replace(ex.Message, """", "") 'Script will error if we don't replace double quotes
errorText = Replace(errorText, "'", "") 'Replace single quotes, too
ClientScript.RegisterStartupScript(Me.[GetType](), "testAlert", "alert('" + errorText + "');", True)
End Try

Asp.net msgbox not trigeering in browser that is not of the server

I am having problem in triggering Msgbox. It is not working in other browser than that of the iis server.
This is my code
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (Not Session("user") = 1) Then
MsgBox("You are not signed in!")
Exit Sub
End If
End Sub
How to resolve this problem.
Thanks
Since you say in the browser, I assume you're using WinForms or MVC, etc. MessageBox doesn't exist in that context. Look into using Javascript or some client-side code to send a message to the user. Something like this should be close:
String cstext = "You are not signed in!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", cstext, true);
or:
ScriptManager.RegisterStartupScript
Good luck.

ELMAH Error Signalling (ASP.NET)

I've got the code below up and running. It captures the user name and the browser that was used to access an intranet site. How can I modify the code so that ELMAH logs a specific StatusCode to the ELMAH_Error table in SQLServer? The eventual goal is the ability to extract out of the log these specific log entries to identify which PC's need to be upgraded.
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started'
Dim ExceptionMessage As New StringBuilder
ExceptionMessage.Append(System.Security.Principal.WindowsIdentity.GetCurrent.Name + " accessed the site using " + Request.Browser.Browser + " " + Request.Browser.MajorVersion.toString + "." + Request.Browser.MinorVersion.tostring)
Dim NewException As New NotSupportedException(ExceptionMessage.ToString)
Elmah.ErrorSignal.FromCurrentContext().Raise(NewException)
End Sub

Resources