Solved

SqlException - is there a specific exception for 'login failed'?

Posted on 2006-11-22
2
279 Views
Last Modified: 2010-04-16
Hi,

I have a login dialogue for an application that connects to a database server.  If the connection.open fails after the user has entered their credentials I handle if with an SqlException and print the message.  I want to be a bit more specific though because I want to handle network failures differently to login failures.  Is there a way of distinguishing the types of exception?  Has anyone got a link to a list of all exceptions, or ways of getting information out of SqlException?

Thanks,

David
0
Comment
Question by:dave_gr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 200 total points
ID: 17995130
Yes, it's 18456

check sql server documentation for errors number

if you need to look for specific error numbers, you can make a test application that return the error numbers than search about them in the sql books online, for example

                  SqlConnection cn = new SqlConnection("Data source=.;user id =sa;password=123;");
                  try
                  {
                        cn.Open();
                  }
                  catch(SqlException ex)
                  {
                        MessageBox.Show(ex.Number.ToString());
                  }

will return the error number, use this number to get the detail of it, then use replace it with your specific error

or you can just use
MessageBox.Show(ex.Message);

this will give you login fail for the user ....


HTH

Best regards,
Mohammed Nasman
0
 
LVL 5

Assisted Solution

by:Collindsouza
Collindsouza earned 50 total points
ID: 17996942
Login Fail error number is 18456

If the server encounters an error that prevents a login from succeeding, the client will display the following error mesage.

Msg 18456, Level 14, State 1, Server <server name>, Line 1
Login failed for user '<user name>'

Note that the message is kept fairly nondescript to prevent information disclosure to unauthenticated clients.   In particular, the 'State' will always be shown to be '1' regardless of the nature of the problem.  To determine the true reason for the failure, the administrator can look in the server's error log where a corresponding entry will be written.  An example of an entry is:

2006-02-27 00:02:00.34 Logon     Error: 18456, Severity: 14, State: 8.

2006-02-27 00:02:00.34 Logon     Login failed for user '<user name>'. [CLIENT: <ip address>]


The key to the message is the 'State' which the server will accurately set to reflect the source of the problem.  In the example above, State 8 indicates that the authentication failed because the user provided an incorrect password.  The common error states and their descriptions are provided in the following table:

ERROR STATE  ERROR DESCRIPTION
 
2 and 5      Invalid userid
6       Attempt to use a Windows login name with SQL Authentication
7       Login disabled and password mismatch
8       Password mismatch
9       Invalid password
11 and 12 Valid login but server access failure
13       SQL Server service paused
18       Change password required

Now.. using try catch you know the type of exception occured and display messages accordingly


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question