Monday, February 20, 2012

question on a stored procedure...

Hi all,
I have a basic question with a stored procedure I'm using. The procedure is as follows:
CREATE PROCEDURE CheckUserLogin(@.Login nvarchar(50), @.Password nvarchar(50)) AS
DECLARE @.UserId varchar(50)
DECLARE @.SchoolId int
DECLARE @.title varchar(50)

IF EXISTS(SELECT Login, Pass FROM Users WHERELogin=@.Login and Pass=@.Password)
BEGIN
SELECT UserId, SchoolId, title FROM Users WHERELogin=@.Login andPass=@.Password
END
ELSE
BEGIN
SELECT @.UserId ='InvalidLogin', @.SchoolId = 0, @.title = 'Applicant'
END
GO
--*******************************************************************--
The thing is that when I read the results, the Else part from the procedure doesn't return anything, so I'm assuming how I've declared the variables or the way I'm selecting them is wrong because if the password and login are correct (this is the first if statement), then when I read the results, I get to where I'm supposed to; however, if the Login and password are incorrect, then I'm supposed to return the Else part...but it doesn't return anything
I have the following when trying to check the userLogin and password

Dim resultAs SqlDataReader

result = CheckUserLoginCmd.ExecuteReader

While result.Read..
...
but I never go into the While loop if the login and password are incorrect, but I do go into the while loop if login and password are correct, and I get the expected behavior. Thanks for your help,

You'd have to use OUTPUT parameters to retrieve that data you are capturing in those @.variables. Try it like this instead:
CREATE PROCEDURE CheckUserLogin(@.Login nvarchar(50), @.Password nvarchar(50)) AS
IF EXISTS(SELECT Login, Pass FROM Users WHERELogin=@.Login and Pass=@.Password)
BEGIN
SELECT UserId, SchoolId, title FROM Users WHERELogin=@.Login andPass=@.Password
END
ELSE
BEGIN
SELECT UserId ='InvalidLogin', SchoolId = 0, title = 'Applicant'
END
GO
|||

Hi,

Thanks for your reply. I actually already tried it like that, but it gave me an error:

Input string was not in a correct format.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.FormatException: Input string was not in a correct format.
Source Error:

Line 130: With userLine 131: If Not IsDBNull(result("UserId")) ThenLine 132: .userId = CType(result("UserId"), String)Line 133: End IfLine 134: If Not IsDBNull(result("SchoolId")) Then

I thought this was because of the way I was returning the result, but maybe the error is something else?...If you have any idea, please let me know. Thanks again.

No comments:

Post a Comment