Solved

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

Posted on 2006-11-22
2
277 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 50
linq .concat equivalent with expression syntax 4 41
Need a quick overview of Selenium testing 2 34
Why am I losing my session variable value 11 35
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

738 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