Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a checkbox in my project to save invoice status (Yes-No)
VB
If ch1.Checked = True Then cmd.Parameters.AddWithValue("@cancelled", "Yes") Else cmd.Parameters.AddWithValue("@cancelled", "No")

Saving and displaying is working normally, but when I try to read the column with datareader as the following
<pre lang="vb">
ch1.Checked = dr!cancelled
<pre lang="vb">


it gives me error

[FormatException: Input string was not in a correct format.]

[InvalidCastException: Conversion from string "No" to type 'Boolean' is not valid.]
Posted

1 solution

Like the Exception message suggests, you are trying to perform an invalid casting.

In my opinion, in database, the status should save the boolean values which can be formatted as "Yes" or "No" according to the needs.

If you want to keep everything else as is, you have to check the value of cancelled and assign the respective boolean value to ch1.Checked. Something like this:

C#
ch1.Checked = dr!cancelled == "Yes"; //Returns true if "Yes", false otherwise.


This is just an example. You can use Methods from the System.String to compare the values and obviously, make the string to either Upper or to Lower case and then perform the comparison.
 
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