Stored procedure Output parameter in c# is returning Empty where as Working fine while Excecuting in SQL












0















I have created a stored procedure where I have a declared an output parameter. It is giving correct result when I execute it in SQL. but I intergrated it in c# code I am getting an Empty object.I am not sure what is the problem.
I have visited so many links but didn't find any appropriate answer.



Here is my C# function Code :-



public bool PreviewAsReviewerButtonEnableDisable(string advId, string userType)
{
bool result = false;

using (RMS_MVCEntities entities = new RMS_MVCEntities())
{
var command = entities.Database.Connection.CreateCommand();
command.CommandText = "[AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@AdvID", advId));
command.Parameters.Add(new SqlParameter("@Type", userType));
command.Parameters.Add(new SqlParameter("@EnableDisable", SqlDbType.Bit));
command.Parameters["@EnableDisable"].Direction = ParameterDirection.Output;

entities.Database.Connection.Open();
var reader = command.ExecuteReader();
result = Convert.ToBoolean(command.Parameters["@EnableDisable"].Value);
entities.Database.Connection.Close();
}
return result;
}


Here is my Sql Stored Procedure :-



ALTER PROCEDURE [AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]
@AdvID nvarchar(20),
@Type nvarchar(20),
@EnableDisable bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

If @Type='CTT Maker'

Begin

Declare @Count int=(select Count(*) from [AdvTax].[AdvTax_Computation] where AdvID=@AdvID and IsActive=1)

If @Count>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End


If @Type='CTT Checker'

Begin

Declare @Count1 int=(select Count(*) from [AdvTax].[AdvTax_Computation_Checker]
where AdvID=@AdvID and IsActive=1)

If @Count1>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End



END


Kindly help.










share|improve this question























  • Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:42











  • Also, please don't count if all you care about is zero or non-zero. That's what exists is for

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:43











  • your proc is missing a return...

    – JohnB
    Nov 16 '18 at 7:44











  • Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

    – Tetsuya Yamamoto
    Nov 16 '18 at 7:44






  • 2





    Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

    – SeM
    Nov 16 '18 at 7:45
















0















I have created a stored procedure where I have a declared an output parameter. It is giving correct result when I execute it in SQL. but I intergrated it in c# code I am getting an Empty object.I am not sure what is the problem.
I have visited so many links but didn't find any appropriate answer.



Here is my C# function Code :-



public bool PreviewAsReviewerButtonEnableDisable(string advId, string userType)
{
bool result = false;

using (RMS_MVCEntities entities = new RMS_MVCEntities())
{
var command = entities.Database.Connection.CreateCommand();
command.CommandText = "[AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@AdvID", advId));
command.Parameters.Add(new SqlParameter("@Type", userType));
command.Parameters.Add(new SqlParameter("@EnableDisable", SqlDbType.Bit));
command.Parameters["@EnableDisable"].Direction = ParameterDirection.Output;

entities.Database.Connection.Open();
var reader = command.ExecuteReader();
result = Convert.ToBoolean(command.Parameters["@EnableDisable"].Value);
entities.Database.Connection.Close();
}
return result;
}


Here is my Sql Stored Procedure :-



ALTER PROCEDURE [AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]
@AdvID nvarchar(20),
@Type nvarchar(20),
@EnableDisable bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

If @Type='CTT Maker'

Begin

Declare @Count int=(select Count(*) from [AdvTax].[AdvTax_Computation] where AdvID=@AdvID and IsActive=1)

If @Count>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End


If @Type='CTT Checker'

Begin

Declare @Count1 int=(select Count(*) from [AdvTax].[AdvTax_Computation_Checker]
where AdvID=@AdvID and IsActive=1)

If @Count1>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End



END


Kindly help.










share|improve this question























  • Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:42











  • Also, please don't count if all you care about is zero or non-zero. That's what exists is for

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:43











  • your proc is missing a return...

    – JohnB
    Nov 16 '18 at 7:44











  • Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

    – Tetsuya Yamamoto
    Nov 16 '18 at 7:44






  • 2





    Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

    – SeM
    Nov 16 '18 at 7:45














0












0








0








I have created a stored procedure where I have a declared an output parameter. It is giving correct result when I execute it in SQL. but I intergrated it in c# code I am getting an Empty object.I am not sure what is the problem.
I have visited so many links but didn't find any appropriate answer.



Here is my C# function Code :-



public bool PreviewAsReviewerButtonEnableDisable(string advId, string userType)
{
bool result = false;

using (RMS_MVCEntities entities = new RMS_MVCEntities())
{
var command = entities.Database.Connection.CreateCommand();
command.CommandText = "[AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@AdvID", advId));
command.Parameters.Add(new SqlParameter("@Type", userType));
command.Parameters.Add(new SqlParameter("@EnableDisable", SqlDbType.Bit));
command.Parameters["@EnableDisable"].Direction = ParameterDirection.Output;

entities.Database.Connection.Open();
var reader = command.ExecuteReader();
result = Convert.ToBoolean(command.Parameters["@EnableDisable"].Value);
entities.Database.Connection.Close();
}
return result;
}


Here is my Sql Stored Procedure :-



ALTER PROCEDURE [AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]
@AdvID nvarchar(20),
@Type nvarchar(20),
@EnableDisable bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

If @Type='CTT Maker'

Begin

Declare @Count int=(select Count(*) from [AdvTax].[AdvTax_Computation] where AdvID=@AdvID and IsActive=1)

If @Count>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End


If @Type='CTT Checker'

Begin

Declare @Count1 int=(select Count(*) from [AdvTax].[AdvTax_Computation_Checker]
where AdvID=@AdvID and IsActive=1)

If @Count1>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End



END


Kindly help.










share|improve this question














I have created a stored procedure where I have a declared an output parameter. It is giving correct result when I execute it in SQL. but I intergrated it in c# code I am getting an Empty object.I am not sure what is the problem.
I have visited so many links but didn't find any appropriate answer.



Here is my C# function Code :-



public bool PreviewAsReviewerButtonEnableDisable(string advId, string userType)
{
bool result = false;

using (RMS_MVCEntities entities = new RMS_MVCEntities())
{
var command = entities.Database.Connection.CreateCommand();
command.CommandText = "[AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]";
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter("@AdvID", advId));
command.Parameters.Add(new SqlParameter("@Type", userType));
command.Parameters.Add(new SqlParameter("@EnableDisable", SqlDbType.Bit));
command.Parameters["@EnableDisable"].Direction = ParameterDirection.Output;

entities.Database.Connection.Open();
var reader = command.ExecuteReader();
result = Convert.ToBoolean(command.Parameters["@EnableDisable"].Value);
entities.Database.Connection.Close();
}
return result;
}


Here is my Sql Stored Procedure :-



ALTER PROCEDURE [AdvTax].[usp_PreviewAsReviewerButton_Enable_Disable]
@AdvID nvarchar(20),
@Type nvarchar(20),
@EnableDisable bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

If @Type='CTT Maker'

Begin

Declare @Count int=(select Count(*) from [AdvTax].[AdvTax_Computation] where AdvID=@AdvID and IsActive=1)

If @Count>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End


If @Type='CTT Checker'

Begin

Declare @Count1 int=(select Count(*) from [AdvTax].[AdvTax_Computation_Checker]
where AdvID=@AdvID and IsActive=1)

If @Count1>0

Begin
set @EnableDisable=1
End

Else

Begin
set @EnableDisable=0
End

End



END


Kindly help.







c# sql stored-procedures






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 7:38









Tejinder SinghTejinder Singh

6681417




6681417













  • Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:42











  • Also, please don't count if all you care about is zero or non-zero. That's what exists is for

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:43











  • your proc is missing a return...

    – JohnB
    Nov 16 '18 at 7:44











  • Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

    – Tetsuya Yamamoto
    Nov 16 '18 at 7:44






  • 2





    Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

    – SeM
    Nov 16 '18 at 7:45



















  • Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:42











  • Also, please don't count if all you care about is zero or non-zero. That's what exists is for

    – Damien_The_Unbeliever
    Nov 16 '18 at 7:43











  • your proc is missing a return...

    – JohnB
    Nov 16 '18 at 7:44











  • Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

    – Tetsuya Yamamoto
    Nov 16 '18 at 7:44






  • 2





    Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

    – SeM
    Nov 16 '18 at 7:45

















Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

– Damien_The_Unbeliever
Nov 16 '18 at 7:42





Why are you calling ExecuteReader when the stored procedure doesn't return any result sets?

– Damien_The_Unbeliever
Nov 16 '18 at 7:42













Also, please don't count if all you care about is zero or non-zero. That's what exists is for

– Damien_The_Unbeliever
Nov 16 '18 at 7:43





Also, please don't count if all you care about is zero or non-zero. That's what exists is for

– Damien_The_Unbeliever
Nov 16 '18 at 7:43













your proc is missing a return...

– JohnB
Nov 16 '18 at 7:44





your proc is missing a return...

– JohnB
Nov 16 '18 at 7:44













Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

– Tetsuya Yamamoto
Nov 16 '18 at 7:44





Use IF EXISTS and IF NOT EXISTS to check data existence. COUNT should only used to count records.

– Tetsuya Yamamoto
Nov 16 '18 at 7:44




2




2





Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

– SeM
Nov 16 '18 at 7:45





Possible duplicate of How to use executeReader() method to retrieve the value of just one cell

– SeM
Nov 16 '18 at 7:45












1 Answer
1






active

oldest

votes


















-2














You can try to assign it directly



var result = (bool)command.ExecuteReader();


Hope this helps.






share|improve this answer





















  • 2





    ExecuteReader will return SqlDataReader, you can't cast it to bool.

    – SeM
    Nov 16 '18 at 7:51













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333377%2fstored-procedure-output-parameter-in-c-sharp-is-returning-empty-where-as-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2














You can try to assign it directly



var result = (bool)command.ExecuteReader();


Hope this helps.






share|improve this answer





















  • 2





    ExecuteReader will return SqlDataReader, you can't cast it to bool.

    – SeM
    Nov 16 '18 at 7:51


















-2














You can try to assign it directly



var result = (bool)command.ExecuteReader();


Hope this helps.






share|improve this answer





















  • 2





    ExecuteReader will return SqlDataReader, you can't cast it to bool.

    – SeM
    Nov 16 '18 at 7:51
















-2












-2








-2







You can try to assign it directly



var result = (bool)command.ExecuteReader();


Hope this helps.






share|improve this answer















You can try to assign it directly



var result = (bool)command.ExecuteReader();


Hope this helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 8:00









SeM

4,66011631




4,66011631










answered Nov 16 '18 at 7:50









NivramNivram

219




219








  • 2





    ExecuteReader will return SqlDataReader, you can't cast it to bool.

    – SeM
    Nov 16 '18 at 7:51
















  • 2





    ExecuteReader will return SqlDataReader, you can't cast it to bool.

    – SeM
    Nov 16 '18 at 7:51










2




2





ExecuteReader will return SqlDataReader, you can't cast it to bool.

– SeM
Nov 16 '18 at 7:51







ExecuteReader will return SqlDataReader, you can't cast it to bool.

– SeM
Nov 16 '18 at 7:51






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333377%2fstored-procedure-output-parameter-in-c-sharp-is-returning-empty-where-as-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly