Solved

Retrieving data from a database to a checkbox

Posted on 2011-09-26
5
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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