DataBinding boolean values from a database

N

Nathan Sokalski

I have several controls in an ItemTemplate in a Repeater that will sometimes
be hidden. I would like to do this by setting the Visible property using
something like:
Visible='<%# DataBinder.Eval(Container.DataItem,"deleted") %>'



However, this is giving me a conversion error. I have tried having the
database return several different values (the db field is of type bit, but I
need to use a case statement because Visible is set to the opposite of the
db field), but still recieve the same error. I know that I can use the
ItemDataBound event, but would prefer not to since I am simply setting a
property, not doing any calculations. The database I am using is SQL Server
2005, and I am using ASP.NET 2.0. Thanks.

Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
 
K

Ken Cox

Hi Nathan,

I suspect you need to cast it as a boolean and then reverse the logic like
this:

Visible='<%# !(bool)DataBinder.Eval(Container.DataItem,"Discontinued")%>'

Full code below.

Ken
MVP [ASP.NET]
Author: ASP.NET 3.5 For Dummies


<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"ProductName") %>'
Visible='<%#
!(bool)DataBinder.Eval(Container.DataItem,"Discontinued")%>'>
</asp:Label><br />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString1 %>"
ProviderName="<%$
ConnectionStrings:NORTHWNDConnectionString1.ProviderName %>"
SelectCommand="SELECT [ProductName], [Discontinued] FROM
[Products]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
 
J

John Bell

Nathan Sokalski said:
I have several controls in an ItemTemplate in a Repeater that will
sometimes be hidden. I would like to do this by setting the Visible
property using something like:
Visible='<%# DataBinder.Eval(Container.DataItem,"deleted") %>'



However, this is giving me a conversion error. I have tried having the
database return several different values (the db field is of type bit, but
I need to use a case statement because Visible is set to the opposite of
the db field), but still recieve the same error. I know that I can use the
ItemDataBound event, but would prefer not to since I am simply setting a
property, not doing any calculations. The database I am using is SQL
Server 2005, and I am using ASP.NET 2.0. Thanks.

Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/


Hi

I assume you are using a case statement in the query behind this? Haave you
tried using 1-col in the query?
John
 
N

Nathan Sokalski

If 1-col means simply passing back the value of the field, I did try that,
which worked (well, except for the fact that I want the opposite of the
value in the database), which is why I am using a case statement to return
the opposite. This is when the problem started. If Ken's idea of returning
the original value, converting it to a bool, and negating it inline works,
then everything will be great. So hopefully I can make it through this
without resorting to the ItemDataBound event. Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top