Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

VS C# Not a valid boolean

I have this loop the examines a check box in a datagridview.  On my development computer it works fine. I just loaded this program onto a target computer to test and if they put a check in the box and this loop is executed we get the message "String was not recognized as a valid Boolean". How do I track down this error? What could it possibly be?

            foreach(DataGridViewRow row in dgvOpenSalesOrders.Rows)
            {
                try
                {
                    if  ( Convert.ToBoolean(((DataGridViewCell)row.Cells[7]).Value.ToString()) == false)
                    {
                        WriteRecord(Convert.ToDateTime(((DataGridViewCell)row.Cells[0]).Value.ToString()), Convert.ToDateTime(((DataGridViewCell)row.Cells[1]).Value.ToString()), ((DataGridViewCell)row.Cells[2]).Value.ToString(),
                         ((DataGridViewCell)row.Cells[3]).Value.ToString(), ((DataGridViewCell)row.Cells[4]).Value.ToString(), ((DataGridViewCell)row.Cells[5]).Value.ToString(),Convert.ToDecimal(((DataGridViewCell)row.Cells[6]).Value.ToString()),Convert.ToBoolean(((DataGridViewCell)row.Cells[7]).Value.ToString()), TRNSDATE, TRNSFLAG);
                    }
                }
                catch (Exception ex)
                {
                    string eMsg = "003: ERROR: " + ex.Message;
                    if (StackTraceWanted) eMsg += "\n" + ex.StackTrace;
                    MessageBox.Show(eMsg);
                }
            }
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>"String was not recognized as a valid Boolean". How do I track down this error? What could it possibly be?

I guess the ToString is returning something other than true or false.  eg. a German language machine **could** be returning wahr or falsch

To track it down you could put a breakpoint and single step the lines of code, inspecting just what is in the string
Avatar of rwheeler23

ASKER

Does SQL have trouble with the '&'. I was just testing line by line and I noticed the first where this crash occurs one of the text fields has an '&' in it.
Never mind about the '&'. What I just discovered is that it fails at the exact same row every time. Is there a limit to how many rows can be in a datagridview? This dataset is date sensitive. If I run it for any entire year it returns 354 records and it will fail. However, If I run it one month at a time it does not fail.
>>If I run it for any entire year it returns 354 records and it will fail.

That ought not to be a problem.  If it always the same row then is there something special in that row?
Yes, but when I get to that row and there are only 30 records as opposed to 354, the loop goes right through with no problem. I think it is something inside SQL because the actual error message is no END Transaction found for Begin transaction. Let me ask the SQL group about this.
I have found what is the problem but I do not know how to fix it. What this program does is shows all the orders prior to a certain date period. The user can they choose whether to archive the orders or not. How this is done is I added a Boolean check box to the 7th column of the dgv. Now I do not how I have done this but when the screen first appears on the screen it shows all the potential records and there is as date value at the bottom. If the user enters a different date I have given them a Display button. When they click on the button the screen is refreshed with only those orders that qualify with the new date. What I just discovered is that until the user clicks the Display button, the column for the checkbox is in the wrong column. It is column 7 but when I inserted break point column 7 has the values for column 6 which are numeric and explain why I get the message about not being a Boolean. Once I click the Display button, which has a dgv.columns.clear command, column 7 is now column 7 which is the Boolean column I added. I have no idea how to correct this. I can include the code if you want to see it. I must have something out of sequence.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me check how to use the name instead. I did not know you could do that.
OK, I am stuck in a loop here and it must be something I am doing wrong. When I change to use the name of the column and not the number of the column, I get the ever popular message "Object not set to an instance of an object". So somehow when I initially open the form, the column checkbox column does not exist but after the user clicks the display button it does even though the user can see and check the box at all times. My dataset has 6 columns and I add a 7th column to the datadridview to allow the user to put a check in the box as an indicator as to whether to include the record or not. This 7th column is not part of the dataset. It is only added to the datagridview. Here is the code:

            DataGridViewCheckBoxColumn colCheckBox = new DataGridViewCheckBoxColumn();
            colCheckBox.HeaderText = "Keep";
            colCheckBox.Name = "colCheck";
            colCheckBox.Width = 36;
            dgvOpenSalesOrders.Columns.Insert(7, colCheckBox);
            dgvOpenSalesOrders.Columns["colCheck"].ValueType = typeof(Boolean);
Thanks for the tips. I am still trying to track down why the user has to click the display button. It must have something to do with how VS opens the form. It appears when it initially opens my check box is not there. So the question would be how do I add the check box and then have the form open. I think I will open a new case for this question.