Solved

Retrieving data from a database to a checkbox

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

18 Experts available now in Live!

Get 1:1 Help Now