Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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:
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.

Posted

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!

Posted

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:

alert("This is my "message", it is cool.");

It should be:

alert("This is my \"message\", it is cool.");

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...