Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
string stryisactive = Convert.ToString(arrLIst[7]);
                if (stryisactive == "true")
                {
                    stryisactive = "1";
                }
                else
                {
                    stryisactive = "0";
                }




System.FormatException: String was not recognized as a valid Boolean. Error



C#
var parameteristaxexempted = new SqlParameter("@istaxexempted", SqlDbType.Bit);
           parameteristaxexempted.Value = istaxexpted;
           myCommand.Parameters.Add(parameteristaxexempted).Value.ToString();



Failed to convert parameter value from a String to a Boolean. i got this error
Posted
Updated 13-Jul-12 0:33am
v2
Comments
jeyamothi 13-Jul-12 5:19am    
arrLIst[7] contain the value as string or boolean
Shemeer NS 13-Jul-12 10:02am    
whats the value in arrLIst[7] ? whats the value in istaxexpted ?

When you send a parameter to sql with datatype "bit", you will have to send a boolean value (not a string). Try parse your string to a bool before you add it to your sql-parameter

C#
var mystring = "true";
bool myBool;
if(bool.TryParse(mystring, out myBool))
{
    // parsing was ok! I can now use my myBool variable!
    parameteristaxexempted.Value = myBool;
}
else
{
    // could not parse mystring to bool.. Something is wrong with the string value
}
 
Share this answer
 
v2
hello
when you want to pass value as bool why you converting it into string than pass again converting bool
you can used like.

C#
if(bool.TryParse(arrLIst[7], out myBool))
{
    // parsing was ok! I can now use my myBool variable!
    parameteristaxexempted.Value = myBool;
}
else
{
    // could not parse mystring to bool.. Something is wrong with the string value
}

i hope your problem will solve if not than comment
 
Share this answer
 
Comments
2011999 13-Jul-12 9:07am    
Failed to convert parameter value from a String to a Boolean. i got this error
Hi,

Rather then having a string and then converting it into string values, Initialize the parameter to assign as Boolean

C#
Boolean stryisactive = Convert.ToBoolean(arrLIst[7]);

/// Not Needed
//                if (stryisactive == "true")
//                {
//                    stryisactive = "1";
//                }
//                else
//                {
//                    stryisactive = "0";
//                }


This should solve your error.
 
Share this answer
 
v2
I think you try to convert the boolean value to string. you can try this line

C#
myCommand.Parameters.AddWithValue("@istaxexempted", parameteristaxexempted);

instead of this line

C#
myCommand.Parameters.Add(parameteristaxexempted).Value.ToString();
 
Share this answer
 
v2
Comments
2011999 13-Jul-12 5:13am    
not working
2011999 13-Jul-12 6:19am    
System.FormatException: String was not recognized as a valid Boolean. Error
jeyamothi 13-Jul-12 6:51am    
istaxexpted has a boolean or string
2011999 13-Jul-12 7:10am    
string
Hi try this:
C#
Boolean stryisactive = Convert.ToBoolean(arrLIst[7]);

And then:
C#
myCommand.Parameters.Add("@istaxexempted", stryisactive);

This will work. Try this.

--Amit
 
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