Error when data binding to repeater "... is not a valid value for Boolean"

Discussion in 'ASP .Net' started by Alan Silver, Jul 7, 2005.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    I am data binding a repeater that has a checkbox in the ItemTemplate.
    The data value coming out of the database is a char(1) field containing
    either "y" or "n". I want to use this value to set/unset the checkbox.

    I tried the following in the ItemTemplate...

    <asp:CheckBox ID="chkShowNpVar" Text=""
    Checked='<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true :
    false)%>' RunAt="server"/>

    but I got an error...

    <%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>
    is not a valid value for Boolean

    Any idea why? More to the point, how do I do this? TIA

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 7, 2005
    #1
    1. Advertising

  2. RE: Error when data binding to repeater "... is not a valid value for

    you missed the hash <%#

    "Alan Silver" wrote:

    > Hello,
    >
    > I am data binding a repeater that has a checkbox in the ItemTemplate.
    > The data value coming out of the database is a char(1) field containing
    > either "y" or "n". I want to use this value to set/unset the checkbox.
    >
    > I tried the following in the ItemTemplate...
    >
    > <asp:CheckBox ID="chkShowNpVar" Text=""
    > Checked='<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true :
    > false)%>' RunAt="server"/>
    >
    > but I got an error...
    >
    > <%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>
    > is not a valid value for Boolean
    >
    > Any idea why? More to the point, how do I do this? TIA
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 7, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    Re: Error when data binding to repeater "... is not a valid value for

    >you missed the hash <%#

    Oops, typo. The real code had the #.

    Thanks anyway, any other ideas?

    >"Alan Silver" wrote:
    >
    >> Hello,
    >>
    >> I am data binding a repeater that has a checkbox in the ItemTemplate.
    >> The data value coming out of the database is a char(1) field containing
    >> either "y" or "n". I want to use this value to set/unset the checkbox.
    >>
    >> I tried the following in the ItemTemplate...
    >>
    >> <asp:CheckBox ID="chkShowNpVar" Text=""
    >> Checked='<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true :
    >> false)%>' RunAt="server"/>
    >>
    >> but I got an error...
    >>
    >> <%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>
    >> is not a valid value for Boolean
    >>
    >> Any idea why? More to the point, how do I do this? TIA
    >>
    >> --
    >> Alan Silver
    >> (anything added below this line is nothing to do with me)
    >>


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 7, 2005
    #3
  4. Alan Silver

    jasonkester Guest

    Re: Error when data binding to repeater "... is not a valid value for

    Sure. 'y' != 1 and 'n' != 0.

    Any reason you're not using a bit field in the database? That's how
    you do that. If you've inherited somebody else's nightmare and simply
    cannot refactor that piece, then you'll need to handle it in the query
    that you're binding to.

    select *
    , case showVar
    when 'y' then 1
    else 0
    end as IsShowVar
    from YourTable


    Good luck!

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/
     
    jasonkester, Jul 7, 2005
    #4
  5. Alan Silver

    Alan Silver Guest

    Re: Error when data binding to repeater "... is not a valid value for

    >Sure. 'y' != 1 and 'n' != 0.

    I know that, but as I understood it, the databinding would expect true
    or false. The database field contains either "y" or "n", so surely

    <%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>

    would come out true of the field were "y" and false otherwise.

    Or am I missing something basic here?

    >Any reason you're not using a bit field in the database?


    Because the database already has the character field set up and too many
    other things depend on it to risk making a basic change now (been going
    for several years).

    > That's how
    >you do that. If you've inherited somebody else's nightmare and simply
    >cannot refactor that piece, then you'll need to handle it in the query
    >that you're binding to.
    >
    >select *
    > , case showVar
    > when 'y' then 1
    > else 0
    > end as IsShowVar
    >from YourTable


    Ah, from which I may presume that the databinding is expecting a 1 or 0,
    not a true or false. That would explain your comment at the top.

    If so, then I find this very odd. Surely the Checked property of a
    checkbox should logically be true or false, not a number? The SDK
    clearly says that it takes a bool, so why do you say it should take a
    number?

    Or did I miss something else? Maybe the same thing twice!!

    Thanks for the reply. Further elucidation would be appreciated.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 11, 2005
    #5
  6. Alan Silver

    jasonkester Guest

    Re: Error when data binding to repeater "... is not a valid value for

    Hang on. We all missed the real reason this is breaking. ASP.NET
    doesn't allow you to put databinding script into tags marked
    runat=server. That's why it's breaking even though your expression is
    technically correct.

    Databound checkbox lists seem to have gotten overlooked when they were
    putting ASP.NET together. There's really no way to simply flag a
    column to act as the checked/unchecked flag. You have to take care of
    this yourself. There are two approaches you can take, via
    onItemDatabound, or by building the list by hand.

    The first approach involves databinding the list as you've already
    done, and hooking up the onItemDatabound event. To pull it off in a
    datagrid, you'll need to drop an extra hidden boundcolumn into your
    grid that holds the value you're looking for. In the ItemDataBound
    handler, you can sniff for that value, find the checkbox and set it.
    It's sort of painful, but here's basically how you'll do it:



    <asp:datagrid id="dgWhatever" Runat="server"
    AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Label ID="lblIsChecked"
    text='<%#DataBinder.Eval(Container.DataItem,"showVar") %>' Runat=server
    Visible=False/>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="yourBox" OnDataBinding="yourBox_ItemDataBound"
    Runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>



    protected void yourBox_ItemDataBound(object sender, EventArgs e)
    {
    CheckBox box = (CheckBox)sender;
    DataGridItem item = (DataGridItem)box.NamingContainer;
    string isChecked = DataBinder.Eval(item.DataItem, 0).ToString();

    if (isChecked == "y")
    {
    box.Checked = true;
    }
    else
    {
    box.Checked = false;
    }
    }





    If you're dealing with an actual CheckBoxList, I find it easier to
    simply build the list by hand rather than databinding. To do this,
    you'll .clear() the list, then spin through your dataset, .add()ing a
    new ListItem for each record, and setting checked state and text for
    each.

    Anyway, sorry to lead you down the wrong path. Hope this gets you
    pointed back in the right direction!

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/
     
    jasonkester, Jul 12, 2005
    #6
  7. Alan Silver

    Alan Silver Guest

    Re: Error when data binding to repeater "... is not a valid value for

    >Hang on. We all missed the real reason this is breaking. ASP.NET
    >doesn't allow you to put databinding script into tags marked
    >runat=server. That's why it's breaking even though your expression is
    >technically correct.


    Grr

    >Databound checkbox lists seem to have gotten overlooked when they were
    >putting ASP.NET together. There's really no way to simply flag a
    >column to act as the checked/unchecked flag. You have to take care of
    >this yourself. There are two approaches you can take, via
    >onItemDatabound, or by building the list by hand.


    I have already resorted to doing it by capturing OnItemDataBound, so
    that's no great hardship. It just seems overkill when all you want is a
    simple checkbox and everything else works fine with data binding.

    Thanks for the reply. At least I know that the way I'm doing is as good
    as it can be, even if MS could have done it better!!

    Ta ra

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 13, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan
    Replies:
    2
    Views:
    2,573
    Jordan
    Feb 10, 2004
  2. =?Utf-8?B?UGF1bCBL?=

    Data binding with a repeater

    =?Utf-8?B?UGF1bCBL?=, Jun 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    499
    =?Utf-8?B?UGF1bCBL?=
    Jun 4, 2004
  3. Dot net work
    Replies:
    1
    Views:
    777
    Dot net work
    Sep 11, 2004
  4. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    Data Binding in a Repeater

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 13, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    562
    Steven Cheng[MSFT]
    Jun 15, 2006
  5. JcFx
    Replies:
    0
    Views:
    317
Loading...

Share This Page