[Last Call] Learn how to a build a cloud-first strategyRegister Now


Bind RadioButtonList to SQL Bit Field

Posted on 2011-10-30
Medium Priority
Last Modified: 2012-05-12
How do you bind a RadioButtonList to a MS SQL database bit field?  I have tried the following without success:

<asp:RadioButtonList ID="SubscribeToNewsletter" runat="server" RepeatDirection="Horizontal" SelectedIndex='<%#Bind("SubscribeToNewsletter")%>'>
   <asp:ListItem Value="True" Text="Yes" />
   <asp:ListItem Value="False" Text="No" />
Question by:cgcmq
LVL 40
ID: 37054203
A bit field is a Boolean. The RadioButtonList expects an integer.

You might be using a RadioButton where you should be using a CheckBox. From my perspective, SubscribeToNewsLetter looks like a request for Yes or No. Most of the time, a CheckBox would be the most appropriate way to handle this in an interface.

Author Comment

ID: 37056039
Good point regarding using a check box for the newsletter.  I do have a couple of other bit fields, such as 'approved/not approved' and 'new/renewal', so I am still looking for a way to bind Radio Buttons to these SQL bit fields.  

Are saying that RadioButtonList cannot bind to a bit Field?
LVL 40
ID: 37056933
Any time that you have as Yes / No or True / False information to gather, the CheckBox is the best option.

Users make the difference between a square box and a round box on the screen.

A RadioButtonList can hold more than 2 RadioButton controls, so it expect a numeric value, not a bit field.

Even if bit fields have the values 0 or 1, these are read as Boolean (0 is False and 1 is True) in .NET, so you would need to convert the value that has been read from Boolean to Integer before you can use it with a RadioButtonList.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 37057091
The radio button differs from a drop down list then does it, because those can bind to bit fields?  

<asp:DropDownList ID="Approved" runat="server" SelectedValue='<%# Bind("Approved") %>'>
  <asp:ListItem Text="Yes" Value="True" />
  <asp:ListItem Text="No" Value="False" />
LVL 27

Accepted Solution

MikeToole earned 1000 total points
ID: 37090397
In the question the SelectedIndex property is the target of the binding which, as JamesBurger comment, needs to bind to an integer, not a bit.
The binding in the DropDownList is to the SelectedValue
    <asp:DropDownList ID="Approved" runat="server" SelectedValue='<%# Bind("Approved") %>'>

Try changing the binding in the question to SelectedValue as you had for the DropDownList, it might work :)
<asp:RadioButtonList ID="SubscribeToNewsletter" runat="server" RepeatDirection="Horizontal" SelectedValue='<%#Bind("SubscribeToNewsletter")%>'>

A RadioButtonList can have more than two items, only one of which  should be selected at any one time, that is why binding SelectedIndex to an integer is the standard pattern.
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 37090607
It's a question of design and standards. CheckBoxes and RadioButtons have been designed for different roles. You are simply not using the right Control.

CheckBoxes have been designed to work with 2 choices. A bit field is True or False, a CheckBox is checked or not. So CheckBoxes are designed to work with bit fields and bind to that type of field.

RadioButtons have been made to work with more than 2 choices. Although you can put only 2 of them on a screen, they are designed for 3 or more. Thus, they are designed to work with an integer and bind to that type of field.

Screen or Page space is costly.  Since you need only 1 CheckBox yes or no (checked or not checked), they are the logical choice. They are also something that any Windows user recognize. Since you need 2 RadioButtons for a yes or no, it takes more space and is thus not a good choice.

When you need a TextBox, a Label won't do.

When you need a CheckBox, a RadioButton won't do.
LVL 12

Expert Comment

ID: 37090829

Author Comment

ID: 37093622
So here is what I have determined,

To bind a RadioButtonList use: SelectedValue NOT SelectedIndex.  

This will bind to an integer: <asp:ListItem Text="Yes" Value="1" />

and to a bit field: <asp:ListItem Text="Yes" Value="True" />
(thank you MikeToole).

I agree that often a check box may be the better option when using bit fields, such as opting into something such as a newsletter (thank you JamesBurger).

There are times though, when using a bit field, that I think a radio button would be the better option.  For example, if you want to ensure that the user has made a choice (explicit consent), a radio button can be validated as a required field.   A check box is implicit consent, or in the case of it not being checked - implicit dissent, as it cannot be validated as a required field.

With this in mind, in my project I have changed 'Subscribe  to newsletter' to a check box.  I have left 'Approve this record' as a radio button with neither button selected at page load but have included a Required Field validator on it.

Thanks for all the help.  I will split the points between the two of you.

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

834 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