Solved

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

Posted on 2006-11-22
2
273 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
2 Comments
 
LVL 22

Accepted Solution

by:
mnasman earned 200 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now