How can I check if a string contains two other strings [closed]

You shouldn’t be using the exception message to determine what has gone wrong. Instead catch the relevant exception type and deal with it in there. For example you likely wnat to use SqlException:

try
{
    ConnectToDatabase();
}
catch(SqlException ex)
{
    //Now we know a SQL exception has occurred, perhaps check the Number property?
    if(ex.Number == 18456) 
    {
        //Login failed
    }
}
catch(Exception ex)
{
    //General exception handling goes here
}

Leave a Comment