Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem getting a returned value from a stored procedure, it's just returning dbnull, I checked the SP and that's working okay

Stored Procedure :-

SQL
alter PROCEDURE UPDATE_SiteVisitors2
	
@VPCode nvarchar(10),


AS

BEGIN

declare @Exists INT 

SET NOCOUNT ON;

IF EXISTS (select vpcode FROM SiteVisitors WHERE vpcode=@vpcode)

BEGIN

update sitevisitors

set

VSignOut = getdate()

where VPCode=@VPCode

		SET @Exists = 1

END

ELSE

BEGIN

		SET @Exists = 0
END

RETURN @Exists

END


VB :-


VB
<pre>    Public Function updateVisitors(ByVal VPCode As String)

        Dim conn As New System.Data.SqlClient.SqlConnection(access.SQLstrconn)
        Dim sql As String = "UPDATE_SiteVisitors2 '" & VPCode & "'"
        Dim Cmd As New System.Data.SqlClient.SqlCommand(sql, conn)
        Dim Par As New System.Data.SqlClient.SqlParameter
        Cmd.Parameters.Add("@Exists", SqlDbType.Int)
        Cmd.Parameters("@Exists").Direction = ParameterDirection.Output

        conn.Open()
        Cmd.ExecuteNonQuery()

        SignIn.exists = Cmd.Parameters("@Exists").Value

        conn.Close()

'signin.exists is a public variable int

        MsgBox(SignIn.exists)

        Return SignIn.exists

    End Function


What I have tried:

Tried a few things but just getting DBNULL
Posted
Updated 5-May-17 3:55am
Comments
CHill60 5-May-17 9:21am    
Is VSignOut actually being updated?
caffrey_1 5-May-17 9:32am    
Yes it is, the update works fine just @Exists is just returned as DBNULL
[no name] 5-May-17 9:27am    
cmd.CommandType = CommandType.StoredProcedure
caffrey_1 5-May-17 9:34am    
Throws a couldn't find stored procedure exception ?
[no name] 5-May-17 9:40am    
That is because the name of your SP is UPDATE_SiteVisitors2 not UPDATE_SiteVisitors2 + someVPCode.

@Exists is not an output parameter, it's a return value
Try the following:
VB
Dim sql As String = "UPDATE_SiteVisitors2"
        Dim Cmd As New System.Data.SqlClient.SqlCommand(sql, conn)
        Cmd.Parameters.Add("@Exists", SqlDbType.Int)
        Cmd.Parameters("@Exists").Direction = ParameterDirection.ReturnValue
        Dim par As SqlParameter = Cmd.Parameters.Add("@VP", SqlDbType.NVarChar)
        par.Direction = ParameterDirection.Input
        par.Value = VPCode
...
 
Share this answer
 
You need to declare your variable in SQL as OUTPUT. A good example is here, Returning Data by Using OUTPUT Parameters[^].

My preference is to NOT use output parameters. Instead of RETURN in your SQL just do SELECT @Exists AS Exists

Then in C#
C#
String returnValue;
Object temp = cmd.ExecuteScalar();
if (temp!= null){
  returnValue = temp.ToString();
}


Either way you go is fine as long as you do it right. ;)
 
Share this answer
 
v2
Comments
caffrey_1 5-May-17 9:36am    
I was considering doing it an alternative way and fill a dataset, if the dataset has no rows etc. Just trying to do it a neater way!
ZurdoDev 5-May-17 9:40am    
Since you just return one value it is my understanding that ExecuteScalar() is more efficient than an output parameter.
Nailed it. was thanks to the
Cmd.CommandType = CommandType.StoredProcedure
pointer

Thanks for the help and pointers much appreciated

New code

VB
Public Function updateVisitors(ByVal VPCode As String)



       Dim conn As New System.Data.SqlClient.SqlConnection(access.SQLstrconn)


       'Dim sql As String = "UPDATE_SiteVisitors2 '" & VPCode & "'"
       Dim sql As String = "UPDATE_SiteVisitors2"
       Dim Cmd As New System.Data.SqlClient.SqlCommand(sql, conn)
       Cmd.CommandType = CommandType.StoredProcedure
       Cmd.Parameters.Add("@VPCode", SqlDbType.VarChar, 10, ParameterDirection.Input).Value = VPCode
       Cmd.Parameters.Add("@Exists", SqlDbType.Int)
       Cmd.Parameters("@Exists").Direction = ParameterDirection.Output
       conn.Open()

       Cmd.ExecuteNonQuery()

       SignIn.exists = Cmd.Parameters("@Exists").Value

       conn.Close()


       MsgBox(SignIn.exists)

       Return SignIn.exists

   End Function


SQL slightly changed
SQL
alter PROCEDURE UPDATE_SiteVisitors2
	
@VPCode nvarchar(10),
@Exists INT OUTPUT

AS

BEGIN

SET NOCOUNT ON;

--declare @Exists INT 

IF EXISTS (select vpcode FROM SiteVisitors WHERE vpcode=@vpcode)


BEGIN

update sitevisitors

set

VSignOut = getdate()

where VPCode=@VPCode


		set @Exists = 1
END
ELSE
BEGIN
		set @Exists = 0
END

RETURN @Exists
END
 
Share this answer
 
v2

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