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.InvalidCastException' 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("Veteran")
rfgrahamAsked:
Who is Participating?
 
ArkConnect With a Mentor Commented:
Oops, sorry - OleDBDatareader.GetValue require column's ordinal, not its name. So, use
If OleDbReader.GetValue(3) 'where 3 is "Veteran" column's ordinal
0
 
John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
Hi....
Try to use an if/then Clause deppending on your Value
If [YourDatabase Value]="Yes" then Veteran.CheckState=CheckState.Checked
0
 
kmslogicCommented:
I think there's something wrong with your reading of the yes/no field.  It sounds like what you're getting with OleDbReader.GetValue("Veteran") is the string "Veteran" and you should be getting a 0 or 1 (maybe -1 I forget) integer value from that query.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CodeCruiserCommented:
You can change your query so it returns 1 or 0 instead of yes/no

Select IIF(fieldname = 'Yes', 1, 0) From TableName
0
 
ArkConnect With a Mentor Commented:
Veteran.Checked = OleDbReader.GetValue("Veteran").Equals("Veteran")
0
 
rfgrahamAuthor Commented:
Using:
Veteran.Checked = OleDbReader.GetValue("Veteran").Equals("Veteran")
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
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
How about the other 2 solutions?
0
 
John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
If i  remember well the field in access is Yes/No..
Did you try
 
If OleDbReader.GetValue("Veteran")="Yes" then Veteran.CheckState=CheckState.Checked

Open in new window

0
 
rfgrahamAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.