473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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)
Nov 19 '05 #1
6 2588
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)

Nov 19 '05 #2
>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)
Nov 19 '05 #3
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/

Nov 19 '05 #4
>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)
Nov 19 '05 #5
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/

Nov 19 '05 #6
>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)
Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Coldman | last post by:
which was saying this: "i have an asp:repeater - its values r initialezed w/ a .DataBind() call does it mean any changes user makes on the page will automaticly go to the database? quite...
6
by: Jurgen Haan | last post by:
Hi hi, I'm trying to perform a redirected restore, but I get a nice error returned. Does anyone have an idea of what the -5130 errorcode means? db2inst@ELMO:~> db2 restore db efproddb into...
0
by: dalaeth | last post by:
I have searched Google high and low and haven't found anything that works. Here's my problem, hopefully someone will be able to help! I'm using 1.1 Framework, and ODP.NET 9.5.0.7 on a Windows...
1
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
0
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
5
by: Cirene | last post by:
I just deployed my new ASP.NET (3.5 FW) site to the hosting company I'm using, webhost4life. NOTE: I HAVE deployed other SQL Server sites to the same account with no issues. Now I'm getting...
1
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.