Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,

I want to show exception in alert box in asp.net 4. But it is not showing any alert. I tried these solutions -
try{// my code}
catch (Exception ex)
{
//Response.Write("alert('" + Server.HtmlEncode(ex.Message) + "')"); // Not Working //Response.Write("alert('" + ex.Message + "');"); // Not working
// Response.Write("alert('" + Server.HtmlEncode(ex.ToString()) + "')"); // Not working //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "done", "alert('Error Occured.');", true); // Only this is working.
}

Please suggest any solution to this where I am going wrong.

Thanks and regards,
Rizwan Gazi.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jun-14 2:27am    
This code is not Javascript. Do you have anything to add? Use "Improve question".
—SA
Rizwan Gazi 25-Jun-14 2:38am    
Nothing to add.

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "CreateExamError", "alert('" + ex.Message.Replace("'", "") + "');", true);

I had to use Replace function in alert.
 
Share this answer
 
Comments
Rizwan Gazi 25-Jun-14 3:57am    
Thanks for considering my question and for your efforts.
C#
  try
   {
     int a, b, c;
     b = 0;
     a = 10;
     c = a / b;

  }
catch (Exception ex)
 {

     Response.Write("<script>alert('"+ex.Message+"');</script>");
 }
 
Share this answer
 
Comments
Rizwan Gazi 25-Jun-14 2:38am    
Dear @Prasad, ex.Message I have tried. But it doesn't work.
Prasad Avunoori 25-Jun-14 2:56am    
But, It worked for me.Just run above code in your PC and let me know.
Rizwan Gazi 25-Jun-14 3:09am    
Your code worked. But SQL Exception, its not showing dear.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900