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

0

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.

System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Login failed for user 'S'.
  Source=.Net SqlClient Data Provider
  StackTrace:
<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))
        {
            con.Open();

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

            con.Close();
        }

        return true;
    }
c#
asp.net
sqlconnection
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