Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The code
Convert.ToBoolean(row.Cells["chkBox"].Value
throws exception
System.FormatException: 'String was not recognized as a valid Boolean.'


I tried bool.Parse and bool.TryParse but in both situations the same exception is throws.

What I have tried:

private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
           
            ArrayList myList = new ArrayList();

         

            for (int i = 0; i < CategoryGV.Rows.Count; i++)
            {
                row = CategoryGV.Rows[i];

                if (Convert.ToBoolean(row.Cells["chkBox"].Value))
                {
                    
                   
                    myList.Add(row.Index);    
                }

            }


            if(myList.Count>0)
            {
                exportSelectedRowsToExcel(myList);
            }
            else
            {
                MessageBox.Show("No rows selected");
            }

        }
Posted
Updated 3-Dec-19 22:11pm
Comments
phil.o 4-Dec-19 3:05am    
Please specify which actual string value you are trying to convert to a boolean.

Quote:
I tried bool.Parse and bool.TryParse but in both situations the same exception is throws.
No, you didn't. TryParse specifically never throws an exception - it always returns a true / false indicator to show if the value was converted or not. If your modified code throws an exception with TryParse, then it's not the line you think it is that causes the problem.

The trouble is, this problem involves your data as well as your code, and we have no access to that. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
CPallini 4-Dec-19 4:20am    
5.
If the cell contains a checkbox, the solution could be a simple as
C#
if (row.Cells["chkBox"].Checked)

Checked property returns a boolean value which you do not have to parse at all.

If the cell contains a string value which the Parse method does not recognize, then you may have to create your own function, taking your actual string values into account:
C#
public static bool CustomBoolParse(string value)
{
   value = value?.ToLower().Trim() ?? string.Empty;
   switch (value)
   {
      case "true":
      case "wahr":
      case "verdad":
      case "vrai":
      case "1":
         return true;
      case "false":
      case "falsch":
      case "falso":
      case "faux":
      case "0":
      default:
         return false;
   }
}

Note these are just examples, as I don't know which actual string value you are trying to parse. Modify the function according to your specific needs.
 
Share this answer
 
v2
Comments
CPallini 4-Dec-19 4:19am    
5.
plesae check what is exact value of "row.Cells["chkBox"].Value" if that is some different string other than true/false then it always going to thrown an exception if you are using Convert.ToBoolean.

safe parsing is always best practice e.g. boolean.tryparse but that is all depended on kind of value you receive, if value is always xyz then you will get false always during parsing.
 
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