문제

I have met the exception System.Data.SqlClient.SqlException when I tried to access a webpage on my ASP.NET web application without any network connection.

I have tried to catch the exception by using a try catch, however it does not work. Below is my code snippet:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]";
        SqlDataSource1.DataBind();
    }
    catch (SqlException ex)
    {
        Response.Redirect("/App/ErrorPage.aspx");
    }
}

My try catch worked on other functions within my webpage itself. E.g. ErrorPage will be shown if user try to delete record when not connected to network. But as for the pageload method, the try catch did not work as expected, and the error showed as the webpage below: enter image description here

Anyone can tell me where I have gone wrong?

Thank you

도움이 되었습니까?

해결책

I'm not sure about the reason but I believe this code will work for you:

try
{
    SqlDataSource1.SelectCommand = "SELECT * FROM [UserDB]";
    SqlDataSource1.DataBind();
}
catch (System.Data.SqlClient.SqlException ex) //Catch SqlException
{
    Response.Redirect("/App/ErrorPage.aspx");
}
catch(Exception ex) //Catch Other Exception
{
    Response.Write(ex.Message);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top