Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using VB 2012 and SQL Server 2012. Written and tested stored procedure to return a value depending upon success of procedure. Stored procedure works perfectly, but I am having difficulty writing the code correctly to retrieve the return value so I can process it.

What keeps getting returned to my variable is a -1. This happens regardless of whether NOCOUNT is set to ON or OFF.

What I have tried:

SQL Server stored procedure:

USE [BEClientData]
GO
/****** Object: StoredProcedure [dbo].[sprocAuthorizeLogin] Script Date: 9/20/2016 9:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Stan Bibbs
-- Create date: 18 Sep 2016
-- Description: Check login submission
-- =============================================
ALTER PROCEDURE [dbo].[sprocAuthorizeLogin]
-- Add the parameters for the stored procedure here
@Username nvarchar(20) = 0,
@Password nvarchar(20) = 0

AS

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


-- Insert statements for procedure here
IF EXISTS(SELECT Username, Password
FROM dbo.SignInTable
WHERE Username = @Username AND Password = @Password)

RETURN 2

ELSE

RETURN 1

END

VB function which calls procedure:

Public Function ValidateLogin(ByVal strUsername As String, ByVal strPassword As String) As Integer

Dim intExists As Integer

Dim cn As New SqlConnection(My.Settings.BEClientDataConnectionString)

Dim procName As String = "sprocAuthorizeLogin"
Dim cmd As New SqlCommand(procName, cn)
With cmd
.CommandType = CommandType.StoredProcedure
.CommandText = procName
.Parameters.Add("@Username", SqlDbType.NVarChar, 20).Value = strUsername
.Parameters.Add("@Password", SqlDbType.NVarChar, 20).Value = strPassword
End With

cn.Open()

intExists = cmd.ExecuteNonQuery()

Return intExists
Posted
Updated 20-Sep-16 6:45am

try this

SQL
ALTER PROCEDURE [dbo].[sprocAuthorizeLogin] 
@Username nvarchar(20) = 0, 
@Password nvarchar(20) = 0
AS
BEGIN
IF EXISTS(SELECT Username, Password FROM dbo.SignInTable WHERE Username = @Username AND Password = @Password)
select 2 
ELSE 
select 1
END


VB
intExists =  Convert.ToInt32(cmd.ExecuteScalar())

refer ExecuteScalar Vs ExecuteNonQuery[^]
 
Share this answer
 
Comments
Member 12750150 21-Sep-16 8:26am    
Solution 3 worked like a charm. Simple syntax adjustment in my stored procedure solved the problem. I had tried ExecuteScalar with my previous syntax and it didn't work either. Didn't test it, but Solution 2 looks like a variation on the same theme as Solution 3. Thanks, Karthik.
Karthik_Mahalingam 21-Sep-16 8:58am    
welcome :)
You're using ExecuteNonQuery for a query. ExecuteNonQuery is designed for INSERTS/DELETES/UPDATES so the value returned is the count of rows affected.
 
Share this answer
 
v2
As far as I know, there is no direct way to get the return value of an SP outside SSMS - ExecuteNonQuery won't do it - it returns the number of rows affected, not the RETURN value. And ExecuteScalar won't either, unless the result its SELECTed.
If you can modify your SP to SELECT the value instead of / as well as RETURNing it, then your are ok. If not them you need to create a second SP to do just that:
SQL
CREATE PROCEDURE spGetTheRETURNValue
AS
BEGIN
   RETURN 666
END
CREATE PROCEDURE spGetTheActualValue
AS
BEGIN
   DECLARE @ret INT
   EXEC @ret = spGetTheReturnValue
   SELECT @ret
END
You can then call the new procedure using ExecuteScalar and retrieve the value that way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900