Application hanging when testing SQL connection

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I have implemented some checks to a settings form in my app to validate some of the data and test DNS resolution and SQL connections.

For some reason, if I purposely enter an incorrect username and password for the SQL database, my test function hangs without catching any exceptions. The app eventually crashes with a 'Managed Debugging Assistant 'ContextSwitchDeadlock' error.

Here is the code for the SQL connection test:

C#:
public static bool TestDatabaseConnection(string Server, string Username, string Password)
        {
            SqlConnectionStringBuilder testbuilder = new SqlConnectionStringBuilder();
            testbuilder["Server"] = Server;
            testbuilder["User"] = Username;
            testbuilder["Password"] = Password;

            using (SqlConnection connection = new SqlConnection(testbuilder.ConnectionString))
            {
                try
                {
                    connection.Open();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return false;
                }
            }
        }

Can anyone think of a reason this might be failing? There must be a way of being able to test the SQL connection with invalid credentials and have it return an exception on failure.
 
Solution
I have implemented some checks to a settings form in my app to validate some of the data and test DNS resolution and SQL connections.

For some reason, if I purposely enter an incorrect username and password for the SQL database, my test function hangs without catching any exceptions. The app eventually crashes with a 'Managed Debugging Assistant 'ContextSwitchDeadlock' error.

Here is the code for the SQL connection test:

C#:
public static bool TestDatabaseConnection(string Server, string Username, string Password)
        {
            SqlConnectionStringBuilder testbuilder = new SqlConnectionStringBuilder();
            testbuilder["Server"] = Server;
            testbuilder["User"] = Username;
            testbuilder["Password"]...
Is this a matter of invalid username/password, or invalid server name? From my experience, the long logon happens in the case of a bad server name. If the client can talk to the server, the invalid password error comes back quickly from my experience.
 
I have implemented some checks to a settings form in my app to validate some of the data and test DNS resolution and SQL connections.

For some reason, if I purposely enter an incorrect username and password for the SQL database, my test function hangs without catching any exceptions. The app eventually crashes with a 'Managed Debugging Assistant 'ContextSwitchDeadlock' error.

Here is the code for the SQL connection test:

C#:
public static bool TestDatabaseConnection(string Server, string Username, string Password)
        {
            SqlConnectionStringBuilder testbuilder = new SqlConnectionStringBuilder();
            testbuilder["Server"] = Server;
            testbuilder["User"] = Username;
            testbuilder["Password"] = Password;

            using (SqlConnection connection = new SqlConnection(testbuilder.ConnectionString))
            {
                try
                {
                    connection.Open();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return false;
                }
            }
        }

Can anyone think of a reason this might be failing? There must be a way of being able to test the SQL connection with invalid credentials and have it return an exception on failure.
It actually turned out to be a silly mistake on my part.

I had a custom message box being displayed in dialog mode and the issue was with that form being shown rather than the database test.

Thanks for the reply
 
Solution

Latest posts

Back
Top Bottom