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:
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.
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.