Alert Message

stewarts

Newcomer
Joined
Apr 8, 2003
Messages
22
Hi,

I have created an alert message that displays as a pop-up window that is supposed to display whenever an error is encountered - I call it in the catch block of my try-catch. (Previously I was using a label on the page and setting it's text value to the error message and changing it's visible property from true to false whenever an error was encountered.) I changed to this much cleaner, better looking option.

My problem is that, when I call it, it displays sometimes and sometimes it doesn't...I don't know why. On the pages where it displays fine, it always works, but on other pages, under other unknown conditions, it never works. If anyone could shed some light, I would appreciate it VERY much. I've put this code in A LOT of pages of my web application, changing over from the label method...it worked in the page I first changed and tested so I made the mass changes...now it's not working in some instances. ??? Below is the code I'm using...

Thanks in advance,
Sue



Try
InsertSql = "Insert into AuthorizedCentralUsers (UserName,MaintOrgData,MaintAgncyData,MaintAccrdtrData,MaintItmDescData,DelAccrdLstRec,MaintUserData,MaintLaptopUserData,MaintSchoolCodeData,MaintComponentData,MaintProponentData,Reports,Password) values('" & UserName & "','" & MaintOrg & "','" & MaintAgncy & "','" & MaintAccrdtr & "','" & MaintDesc & "','" & DelASLRec & "','" & MaintCenUser & "','" & MaintLaptopUser & "','" & MaintSC & "','" & MaintComp & "','" & MaintProp & "','" & Rpts & "','" & HashedPassword & "')"
MyInsertCmd = New SqlClient.SqlCommand(InsertSql, DBConn)
DBConn.Open()
MyInsertCmd.ExecuteNonQuery()
'ASPNET_MsgBox("User record added successfully.")
ViewState("MainRecDone") = 1
Catch exc As Exception
WriteError = True
ASPNET_MsgBox("ERROR SAVING NEW RECORD >>> (" & exc.Message & ")")
End Try



Public Sub ASPNET_MsgBox(ByVal Message As String)

System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" & vbCrLf)
System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

End Sub
 
There's a much easier way to handle error checking in ASP.NET. You need to add a global.asax file to your website and then add the Application_Error method. In this method you can simply perform a Response.Redirect to a page where you call the "GetLastError" method for the current HttpServer. This would look something like:
C#:
Exception ex = Server.GetLastError();

if (ex != null)
{
    // print it to the screen            
}
This way you do not have to wrap numerous chunks of code with try/catch blocks.
 
Thanks for your reply, Gill Bates. Since I already have so many of these conditions in my code, I thought it would be quicker at this point to just figure out what's wrong with the alert message. It seems the exc.message contains something which cannot be displayed, thus causing the entire alert display to fail. When I removed the exc.message portion of the message to be displayed, it worked fine. What I ended up doing is parsing the exc.message up to the first period and then displaying just that part of the exc.message...and that worked okay.

Thanks again!
 
Well it's probably blowing up on a quote. Since your string in the javascript alert is wrapped in double quotes any double quote in the "Message" string will need to be escaped. Some types of Exceptions have quotes in their Message properties.

This will blow up in javascript:
Code:
alert("This is my "message", it is cool.");
It should be:
Code:
alert("This is my \"message\", it is cool.");
 
Back
Top