rfgraham
asked on
how to populate a checkbox from an access database (Data Type Yes/No) in vb 2008
I'm using the following code to get and display the Yes/No data type value from an Access database. This is the error I'm getting:
An unhandled exception of type 'System.InvalidCastExcepti on' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "Veteran" to type 'Integer' is not valid.
How would I convert the string to an integer so the checkbox on my form can display a check symbol if the value is true?
Veteran.CheckState = OleDbReader.GetValue("Vete ran")
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Conversion from string "Veteran" to type 'Integer' is not valid.
How would I convert the string to an integer so the checkbox on my form can display a check symbol if the value is true?
Veteran.CheckState = OleDbReader.GetValue("Vete
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think there's something wrong with your reading of the yes/no field. It sounds like what you're getting with OleDbReader.GetValue("Vete ran") is the string "Veteran" and you should be getting a 0 or 1 (maybe -1 I forget) integer value from that query.
You can change your query so it returns 1 or 0 instead of yes/no
Select IIF(fieldname = 'Yes', 1, 0) From TableName
Select IIF(fieldname = 'Yes', 1, 0) From TableName
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Using:
Veteran.Checked = OleDbReader.GetValue("Vete ran").Equa ls("Vetera n")
Is still returning the error: Conversion from string "Veteran" to type 'Integer' is not valid.
The value being returned from the query is {0} so I'm not sure if the check box on the form is requiring a text value or not. If it is then the integer needs to be converted to a string value. Any idea of how that would be done?
Thanks
Veteran.Checked = OleDbReader.GetValue("Vete
Is still returning the error: Conversion from string "Veteran" to type 'Integer' is not valid.
The value being returned from the query is {0} so I'm not sure if the check box on the form is requiring a text value or not. If it is then the integer needs to be converted to a string value. Any idea of how that would be done?
Thanks
How about the other 2 solutions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I came up with this solution and it works fine
If cboxVeteran.Checked <> OleDbReader("Veteran") Then cboxVeteran.CheckState = CheckState.Checked _
Else cboxVeteran.CheckState = CheckState.Unchecked
Thanks for all your help
If cboxVeteran.Checked <> OleDbReader("Veteran") Then cboxVeteran.CheckState = CheckState.Checked _
Else cboxVeteran.CheckState = CheckState.Unchecked
Thanks for all your help