Bind RadioButtonList to SQL Bit Field

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" />
Who is Participating?
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.
Jacques Bourgeois (James Burger)PresidentCommented:
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.
cgcmqAuthor Commented:
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?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jacques Bourgeois (James Burger)PresidentCommented:
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.
cgcmqAuthor Commented:
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" />
Jacques Bourgeois (James Burger)PresidentCommented:
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.
cgcmqAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.