Try and Catch not working on SQL open command

I would like to initiate my page by checking authentication using SQL Database connection. This is the code I have, and I can't figure out why it's not working:

public partial class SiteMaster : System.Web.UI.MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // Try to open database.
    try
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.ConnectionStrings["blahblah"].ConnectionString;
        con.Open();
        con.Close();
        //successful login, do nothing.
    }
    catch (Exception err) //if error Opening, catch the error, and display
    {
        SiteMasterMsg.Text = "Could not login in using ___.";
        SiteMasterMsg.Text += err.Message;
    }

The problem is the page just dies, and gives me the default IIS error page showing the database connection login failed. My hope was that it would "try" to connect, and then on a failed login attempt, catch the error, and display the page with the error. I have this on my Default.aspx page:

<asp:Label id="SiteMasterMsg" runat="server" />

I can't figure out what's wrong. Am I missing something, or is there perhaps a better way to do this?

Thanks in advance!

Answers


I realized the problem was because this code was on my Site.Master.cs page, so the page would never really fully develop, it would stop at some level of error, and that's what I was seeing. The solution has been to make a dedicated Login.aspx page, and not to put this code in the Site.Master.cs page.


Need Your Help

Access data from object instead of saving it using saveAsTextFile

java scala apache-spark

In the below code snippet, saveAsTxtFile would write it to a file in comma separated format.

Jekyll is ignoring Liquid tags within posts

ruby markdown jekyll liquid pygments

I'm new to Jekyll, Liquid and Ruby as a whole, so sorry if I'm being really stupid. It seems that Jekyll is ignoring any liquid tags with posts (under the _posts directory), which results in output...