Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
The stored proc has a bit output parameter (@Success) which returns 0 or 1

My c# code gives the following error when I want to retrieve the value of @Success. What should I change in my code to solve this error please?

"String was not recognized as a valid boolean"

bool blnSuccess = bool.Parse(comm.Parameters["@Success"].Value.ToString());

Thanks
Posted

You cannot use bool.Parse to convert "0" or "1" to a true/false result: MSDN[^] says so quite clearly.

Instead look at using something like:
C#
bool blnSuccess = comm.Parameters["@Success"].Value.ToString() == "1" ? true : false;
 
Share this answer
 
v2
Comments
lukeer 17-Oct-11 4:55am    
Shouldn't that be the other way round?
arkiboys 17-Oct-11 6:43am    
Thank you
Matt T Heffron 9-Jul-14 16:21pm    
Why are you using a boolean value expression to choose boolean constants that have the same value as the expression?
Why not just:
bool blnSuccess = comm.Parameters["@Success"].Value.ToString() == "1";
And if the .Value is actually numeric then why not:
bool blnSuccess = (int)(comm.Parameters["@Success"].Value) == 1;
Try this...

C#
string i = comm.Parameters["@Success"].Value.ToString();
bool b = bool.Parse(i == "1" ? "true" : "false");
//OR

bool b = i== "1" ? true : false;
 
Share this answer
 
Comments
lukeer 17-Oct-11 4:52am    
I prefer the //OR version.
But still I think it should read
bool b = ((i == "0")? false : true);

I think the parentheses make it more readable.
make sure comm.Parameters["@Success"] is not null by putting a breakpoint at there and observe its value
 
Share this answer
 
bool blnSuccess = comm.Parameters["@Success"].Value=="1"?true:false
 
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