String was not recognized as a valid Boolean

String was not recognized as a valid Boolean


String was not recognized as a valid Boolean


 The string representation of boolean values ("true" , "False" ) can be parsed to compare with actual boolean values (true , false).
If the input string contains invalid format of boolean values ("1", "0" or Null ), you get an unhandled exception as "String was not recognized as a valid Boolean"

  protected void Button1_Click(object sender, EventArgs e)
    {

        string strCheck = "True";

        if (bool.Parse(strCheck))
        {
            Response.Write("Working on");
        }     

    }
    
Output: Working on

  In above example I have used string "xyz" and when application starts to analyse the string content
it throws an exception.

Code:

  protected void Button1_Click(object sender, EventArgs e)
    {
        string strCheck = "xyz";
        
        bool result;

        if (bool.TryParse(strCheck,out  result ))
        {
            Response.Write("Working on");
        }

        Response.Write(result.ToString());
    }      
  
Output: False

Description:
TryParse method accepts two parameters first one is actual string and second is parsed results.
If the string is in ininvalid format or Null, result comes False.

It also handles exception of type
Null reference exception was unhandled

 

 

 

Tags:

C# DateTime ToString().net String vs stringAsp.net Convert String To ImageString was not recognised as a valid datetimeReplace string in C#[C#] Remove Last Character From StringDateTime format Time StringSplit String In C#Linq Convert String Array To Int ArrayHow to Pass To Query String In Asp.netHow To Declare and access array of string in C#

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.