Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
in my asp.net web-form project there is .cs file (code file) where i did database related connections and CRUD operation. Now I want to handle a exception if occurred, I used try catch block. When exception occurred it will be thrown but I want to show message to a user that perticulare exception occured. I know there is a option of javascript but it is just a code file no page is there and no scriot manager is tthere. Please help how to do this

What I have tried:

in my asp.net web-form project there is .cs file (code file) where i did database related connections and CRUD operation. Now I want to handle a exception if occurred, I used try catch block. When exception occurred it will be thrown but I want to show message to a user that perticulare exception occured. I know there is a option of javascript but it is just a code file no page is there and no scriot manager is tthere. Please help how to do this
Posted
Updated 19-Jan-23 15:55pm
Comments
Karthik_Mahalingam 5-May-16 23:59pm    
what do you mean by "no page is there" ?
you want to display the exception as alert message ?
Kishor-KW 6-May-16 0:20am    
No aspx page is there.
ScriptManager.RegisterStartupScript(this.page,page.GetType(),"showalert","alert('Only alert Message');",true);

here we use page but no aspx page is there. and scriptManager is also not there.
Karthik_Mahalingam 6-May-16 0:35am    
refer my solution.

You can easily inject javascript codes from .cs to the aapx page. Ty that. I think that is what you need.
 
Share this answer
 
Comments
Kishor-KW 6-May-16 0:29am    
there is no aspx page
They what you have for front end?
All you have to do is, throw the error in the child class.

Suppose you have class called Business as

C#
public class Business
  {
      public string SomeMethod()
      {
          try
          {
              int x = 0;
              int a = 5 / x;
          }
          catch (Exception)
          {

              throw;  // YOu have to throw the error to parent
          }
          return "";
      }
  }

and you are calling it from a page..

C#
protected void Page_Load(object sender, EventArgs e)
      {

          try
          {
              Business obj = new Business();
              obj.SomeMethod();
          }
          catch (Exception ex)
          {
              ShowMessage(ex.Message);
          }



      }

Helper funciton to show message from cs file

C#
public void ShowMessage(string message)
     {

         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         sb.Append("<script type = 'text/javascript'>");
         sb.Append("window.onload=function(){");
         sb.Append("alert('");
         sb.Append(message);
         sb.Append("')};");
         sb.Append("</script>");
         ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
     }
 
Share this answer
 

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