Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to pass null value to Stored procedure from where i am calling in controller. I am passing it as null but not getting correct records. how to do it?

C#
// my code
//in stored procedure that parameter declred as null
Alter Procedure Proc
 (@flag bit= Null)
As Begin

if @flag is null
 //my select statement
end

//in controller
var data=_entites.GetData(null)//calling SP in controller 


want to pass null for some conditions
is it wirte?
Posted
Updated 26-Aug-15 1:05am
v2
Comments
Maciej Los 26-Aug-15 7:07am    
Have you tried to call sp from Sql Server Menagement Studio? Does it return correct recordset?
Member 9579525 26-Aug-15 7:54am    
Yes. I have tested in Sql server. It return correct records
F-ES Sitecore 26-Aug-15 7:23am    
You might need GetData(DBNull.Value) rather than GetData(null).
Member 9579525 26-Aug-15 7:58am    
I tried that but it is giving error as "Method has invalid arguments". The datatype is bool for that variable.

1 solution

On the first look - if statement in sp is incorrect.

It should look like:

SQL
DECLARE @flag BIT = NULL

IF (@flag IS NULL)
    PRINT 'Flag is null!'


In case when there is more than 1 line after if statement, you have to "wear" this in BEGIN .. END statement.
SQL
IF (@flag IS NULL)
BEGIN
    PRINT 'Flag is null!'
    PRINT 'Please stop passing null values!'
END 


See: IF...ELSE (Transact-SQL)[^]

Note: please, read F-ES Sitecore[^] comment to the question.
 
Share this answer
 
Comments
Member 9579525 26-Aug-15 8:34am    
Thank you. I tried this as well. But not working.
How to pass that null value from controller to SP. Actually it is working in Sql server

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