Bind RadioButtonList to SQL Bit Field

Posted on 2011-10-30
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

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    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

    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

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    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.

    Author Comment

    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

    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)
    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


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now