Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-22
2
Medium Priority
?
284 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 800 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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