How to handle invalid login details to a SQL database from a webapp


I have a web app which contains a modal for the use to enter the details to the SQL server they want to connect to. If the user enters correct details it works as intended.

Currently if a user enters incorrect value I get the following error.

  Message=Login failed for user 'S'.
  Source=.Net SqlClient Data Provider
<Cannot evaluate the exception stack trace>

I am trying to handle the case of the user entering incorrect log in details. The details being passed through are server, database, username and password. My expected result would be that the user is alerted if they enter invalid details

The desired outcome would be for the modal to display that the connection details have been entered incorrectly.

This is the test connection method where I hit the error on con.Open

public static bool TestConnection(string connectionString)
        using (SqlConnection con = new SqlConnection(connectionString))

            if (con.State != ConnectionState.Open)
                Console.WriteLine("Hit an error"); 
                return false;


        return true;
asked on Stack Overflow Oct 28, 2020 by Brandon Hogg • edited Oct 28, 2020 by mason

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0