Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Error when data binding to repeater "... is not a valid value for Boolean"

Reply
Thread Tools

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

 
 
Alan Silver
Guest
Posts: n/a
 
      07-07-2005
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)
 
Reply With Quote
 
 
 
 
=?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=
Guest
Posts: n/a
 
      07-07-2005
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)
>

 
Reply With Quote
 
 
 
 
Alan Silver
Guest
Posts: n/a
 
      07-07-2005
>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)
 
Reply With Quote
 
jasonkester
Guest
Posts: n/a
 
      07-07-2005
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/

 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      07-11-2005
>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)
 
Reply With Quote
 
jasonkester
Guest
Posts: n/a
 
      07-12-2005
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,"showV ar") %>' 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/

 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      07-13-2005
>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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help: "Specified cast is not valid" error when binding the Visible propery of a hyperlink control keithb ASP .Net 4 10-17-2008 02:12 PM
Can I use the value of a bound data item within repeater, without binding it? mark4asp ASP .Net 1 12-03-2007 06:51 PM
Complex data binding question, binding child objects of a custom collection. JcFx ASP .Net Datagrid Control 0 06-01-2005 04:01 PM
Data Binding - using inline code vs. functions vs. straight binding Jordan ASP .Net 2 02-10-2004 08:32 PM
value binding and function binding Vivek Nallur Ruby 0 09-25-2003 02:52 AM



Advertisments