473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Return error codes from Sql Server

Hi all,

I using Inline Sql statements to validate username and password against
sql server. I want to catch any errors from Sql server such as Username not
known, Password incorrect, etc. Is there anyway to do this? Below is the
code I'm using:
Dim oDR As SqlClient.SqlDataReader
Dim sSQL As String

sSQL = "SELECT password from tbl_users WHERE (username ='" &
Trim(Me.txtUserName.Text) & "')"

Dim oCommand As New SqlClient.SqlCommand()
Try
oCommand.CommandText = sSQL
oCommand.Connection = DatabaseConnection
oCommand.CommandType = CommandType.Text
oDR = oCommand.ExecuteReader(CommandBehavior.SingleResul t)
Dim intRA As Integer = oDR.RecordsAffected()
Catch oX As Exception

MessageBox.Show(oX.Message)

Finally
oCommand = Nothing
End Try

Thanks in advance,
Bill
Nov 21 '05 #1
1 1678
SQL Server won't know if your database logins password is incorrect you'll
have to build logic to determine that. You should use a stored procedure that
returns an output parameter indicating a reason code. Here's an example

CREATE PROC esp_ValidateLogin
@pUser VARCHAR(50),
@pPwd VARCHAR(50),
@pReason INT OUTPUT
AS
SELECT UserID from tbl_users WHERE username = @pUser and password = @pPwd
if @@ROWCOUNT = 0
BEGIN
SET @pReason = 1 --default to bad password
DECLARE @UserExists
SELECT @UserExists = COUNT(*) WHERE username = @pUser
IF @UserExists = 0
SET @pReason = 2 --bad user name
END

You should
"William Oliveri" wrote:
Hi all,

I using Inline Sql statements to validate username and password against
sql server. I want to catch any errors from Sql server such as Username not
known, Password incorrect, etc. Is there anyway to do this? Below is the
code I'm using:
Dim oDR As SqlClient.SqlDataReader
Dim sSQL As String

sSQL = "SELECT password from tbl_users WHERE (username ='" &
Trim(Me.txtUserName.Text) & "')"

Dim oCommand As New SqlClient.SqlCommand()
Try
oCommand.CommandText = sSQL
oCommand.Connection = DatabaseConnection
oCommand.CommandType = CommandType.Text
oDR = oCommand.ExecuteReader(CommandBehavior.SingleResul t)
Dim intRA As Integer = oDR.RecordsAffected()
Catch oX As Exception

MessageBox.Show(oX.Message)

Finally
oCommand = Nothing
End Try

Thanks in advance,
Bill

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter | last post by:
Hi, I have the following scenarios: client/server programs implemented with winsock are installed on all systems inthe network (assume it contains s1, s2, and s3, and other systems). If I send a...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
1
by: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan...
3
by: c# beginner | last post by:
we are trying to standardize return codes across our .NET applications (that are soon to be developed.) What is the best practice for standardizing return codes? I know of only the following...
1
by: Lars Netzel | last post by:
I need to use a few WIN API functions and get Return values as Long and Integers from those.. how do I convert these to the error codes so I get to know what error I really got? Example from...
2
by: Rasika WIJAYARATNE | last post by:
Hello, I would like to know what the best way to return error codes to the caller from a webservice would be. Here are my situations for returning error codes: 1. Arguments to web method were...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
3
by: asearle | last post by:
Hallo everyone, I have a perl script which can return error codes. I have done this with, for example, 'exit 11;', which would (I imagine) send the exit code 11 (instead of 0) back to the calling...
8
by: sloan | last post by:
Does anyone have any good articles on Exception Handing in DotNet. As a "get rid of the API mode of knowing if something worked, we're not in VB6 land anymore Dorothy". I have a few people at...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.