Hi,
How can I pass an integer as null into a stored procedure.
The calling sp code:
Public Function GetAccountManagers(ByVal AgencyID As Integer, _
ByVal UserID As Nullable(Of Integer)) As SqlDataReader
Dim db As SqlDatabase = DirectCast(DatabaseFactory.CreateDatabase("My.MySettings.Test_AdminConnectionString"), SqlDatabase)
Dim dbCommand As SqlCommand = db.GetStoredProcCommand("common_GetAccountManagers")
Dim dbDataReader As SqlDataReader
Try
db.AddInParameter(dbCommand, "AgencyID", SqlDbType.Int, AgencyID)
'#001 TSO 25/6/7 Add UserID Paramatre.
If UserID.HasValue = True Then
db.AddInParameter(dbCommand, "UserID", SqlDbType.Int, UserID)
End If
dbDataReader = db.ExecuteReader(dbCommand)
Return dbDataReader
Catch exsql As SqlException
Throw New ApplicationException(exsql.Message, exsql)
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
db = Nothing
dbCommand = Nothing
dbDataReader = Nothing
End Try
End Function
The stored procedure code :
ALTER PROCEDURE common_GetAccountManagers
(
@AgencyID AS INT,
@UserID AS INT = NULL
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT TOP 100 PERCENT UserID AS AdminUserId, ISNULL(UserName, '') + ISNULL(' ' + UserSecondName, '') AS Name, AgencyID, UserEmail
FROM [Security].[dbo].view_AgencyUsers
WHERE 1 = 1
AND (UserLabel = 'agency user')
AND (AgencyID = @AgencyID)
AND (@UserID IS NULL OR UserID = @UserID)
ORDER BY Name
END
As you can see the @UserID is a optional value. Notice the statement where I only set it if not NULL.
The problem, if I pass 0 as Nullable(of integer) through it is 0 which gets passed to sp. Is it possible to send a null value through Nullable(Of Integer)??
I can get around this by added an IF in the calling code to check if UserID = 0 and then set NULL, but this seems bodgy?
Thanks
Tim.
How can I pass an integer as null into a stored procedure.
The calling sp code:
Public Function GetAccountManagers(ByVal AgencyID As Integer, _
ByVal UserID As Nullable(Of Integer)) As SqlDataReader
Dim db As SqlDatabase = DirectCast(DatabaseFactory.CreateDatabase("My.MySettings.Test_AdminConnectionString"), SqlDatabase)
Dim dbCommand As SqlCommand = db.GetStoredProcCommand("common_GetAccountManagers")
Dim dbDataReader As SqlDataReader
Try
db.AddInParameter(dbCommand, "AgencyID", SqlDbType.Int, AgencyID)
'#001 TSO 25/6/7 Add UserID Paramatre.
If UserID.HasValue = True Then
db.AddInParameter(dbCommand, "UserID", SqlDbType.Int, UserID)
End If
dbDataReader = db.ExecuteReader(dbCommand)
Return dbDataReader
Catch exsql As SqlException
Throw New ApplicationException(exsql.Message, exsql)
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
Finally
db = Nothing
dbCommand = Nothing
dbDataReader = Nothing
End Try
End Function
The stored procedure code :
ALTER PROCEDURE common_GetAccountManagers
(
@AgencyID AS INT,
@UserID AS INT = NULL
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT TOP 100 PERCENT UserID AS AdminUserId, ISNULL(UserName, '') + ISNULL(' ' + UserSecondName, '') AS Name, AgencyID, UserEmail
FROM [Security].[dbo].view_AgencyUsers
WHERE 1 = 1
AND (UserLabel = 'agency user')
AND (AgencyID = @AgencyID)
AND (@UserID IS NULL OR UserID = @UserID)
ORDER BY Name
END
As you can see the @UserID is a optional value. Notice the statement where I only set it if not NULL.
The problem, if I pass 0 as Nullable(of integer) through it is 0 which gets passed to sp. Is it possible to send a null value through Nullable(Of Integer)??
I can get around this by added an IF in the calling code to check if UserID = 0 and then set NULL, but this seems bodgy?
Thanks
Tim.