?
Solved

Retrieving data from a database to a checkbox

Posted on 2011-09-26
5
Medium Priority
?
219 Views
Last Modified: 2012-05-12
Hi Everyone,

I am currently working on a mini web application that allows a user to input data into a database and then do a search on these records and bring it back up for editing purposes.  

I am able to retrieve data into textboxes and labels however, i don't know where to begin to retrieve a checkbox.  

Currently, the submit form have 4 checkboxes (i.e. CB1, CB2, CB3, CB4) that a user can select.  A user can select more than one checkboxes.  When i go into the search link, i can't retrieve the data so that whatever the user selected reflects as a checked checkbox.  

Can someone give me some insight as to how to start this?

Thanks!
0
Comment
Question by:sarah_siu
  • 3
  • 2
5 Comments
 
LVL 40
ID: 36601975
Use a bit field for each checkbox in the database.

A bit field is the same as a Boolean in VB.

And what does the CheckBox wants in its Checked property? A Boolean.

You can record the Checked property of each of the CheckBoxes in the bit fields, and then read the bit fields back and use them to set the Checked property of the CheckBoxes, the same way that you set the Text properties of the Labels and TextBoxes.
0
 

Author Comment

by:sarah_siu
ID: 36709566
Hi JamesBurger,

Thank you for your reply.

So just to make sure i understand correctly - i should have the field capturing these checkboxes as a bit field (so either 1/0).  and my retrieval command would look something like this?

Checkbox1.Checked = ds.Tables(0).Rows(0)("checkboxfield1").ToString()

Would that work?

Thanks!
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 36710928
Almost, but it seems that I went to far when I said "the same way that you set the Text properties".

The fields in a table are of the generic type Object. The properties of the controls are of a specific type. You thus have to convert the Object to the property in order to be able to use it in a property.

Your line may work, maybe not, depending on how the Visual Studio environment is set for your project. To be sure, I would however use what follows instead.

The Text property is a String, so you need to convert the Object returned to a String, thus the ToString().

The Checked property is a Boolean however, so you need to convert the Object returned to a Boolean, with the CBool instruction.

Checkbox1.Checked = CBool(ds.Tables(0).Rows(0)("checkboxfield1")) 'CBool instead of ToString.


0
 

Author Closing Comment

by:sarah_siu
ID: 36710963
I loved the way you explained it - it was very clear to me and now it works on my web form.

Much thanks =)
0
 
LVL 40
ID: 36711336
I would add that the same holds true for any other type of value. CInt for an Integer, CDate for a date...
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question