locked
Display Exception Error in Popup using Try Catch in c# RRS feed

  • Question

  • User-797751191 posted

    Hi

      How to Display Exception Error in Popup using Try Catch  . Database used is Sql

    Thanks

    Sunday, June 23, 2019 1:03 PM

Answers

  • User-719153870 posted

    Hi jsshivalik,

    Popping up error messages instead of throwing can make the debugging process more continuous.

    You want to know how to use Try Catch to popup the error message.

    Please refer to below codes:

    CS:

    protected void Button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ddd;Integrated Security=True"))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("insert into Purchase values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"' ) ", conn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch(Exception ex)
                    {
                        Response.Write("<script>alert('"+ex.Message.Replace("\'"," ")+"')</script>");
                    }
                }
            }

    Here's result of my demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 3:01 AM

All replies

  • User1120430333 posted

    jsshivalik

    Hi

      How to Display Exception Error in Popup using Try Catch  . Database used is Sql

    Thanks

    You could use Global Exception Handling at the UI project, you could have no try/catch in any code anywhere that makes any exception thrown an unhandled exception, the unhandled exception is caught by the GEH and the GEH logs the exception with the stack trace and all to a logfile that you review, and the GEH redirects to an user friendly error page.   

    You don't ever what to show exceptions to the user that could expose cortical information to a hacker.

    Sunday, June 23, 2019 1:16 PM
  • User-719153870 posted

    Hi jsshivalik,

    Popping up error messages instead of throwing can make the debugging process more continuous.

    You want to know how to use Try Catch to popup the error message.

    Please refer to below codes:

    CS:

    protected void Button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ddd;Integrated Security=True"))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("insert into Purchase values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"' ) ", conn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch(Exception ex)
                    {
                        Response.Write("<script>alert('"+ex.Message.Replace("\'"," ")+"')</script>");
                    }
                }
            }

    Here's result of my demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 24, 2019 3:01 AM