Solved

Retrieving data from a database to a checkbox

Posted on 2011-09-26
5
196 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 500 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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