Solved

Retrieving data from a database to a checkbox

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now