473,396 Members | 1,996 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,396 software developers and data experts.

DataBinding boolean values from a database

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
nj********@hotmail.com
http://www.nathansokalski.com/
Aug 29 '08 #1
3 3922
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,"Discont inued")%>'

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,"Discont inued")%>'>
</asp:Label><br />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString1 %>"
ProviderName="<%$
ConnectionStrings:NORTHWNDConnectionString1.Provid erName %>"
SelectCommand="SELECT [ProductName], [Discontinued] FROM
[Products]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>

"Nathan Sokalski" <ns*******@kyledavidgroup.comwrote in message
news:u2**************@TK2MSFTNGP05.phx.gbl...
>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
nj********@hotmail.com
http://www.nathansokalski.com/

Aug 29 '08 #2


"Nathan Sokalski" <ns*******@kyledavidgroup.comwrote in message
news:u2**************@TK2MSFTNGP05.phx.gbl...
>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
nj********@hotmail.com
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
Aug 29 '08 #3
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"John Bell" <jb************@hotmail.comwrote in message
news:2A**********************************@microsof t.com...
>
"Nathan Sokalski" <ns*******@kyledavidgroup.comwrote in message
news:u2**************@TK2MSFTNGP05.phx.gbl...
>>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
nj********@hotmail.com
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


Aug 29 '08 #4

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

Similar topics

3
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method,...
6
by: Nathan Sokalski | last post by:
I am using a DataSet as the DataSource of a DataList in my code. The SQL used to get the data from the database begins with: SELECT...
1
by: Tom | last post by:
Hi there I have a little problem here, wich i think is quite strange... I have a dataset, filled from my SQL database. In it are some boolean fields (or bit as SQL server calls them) Now, on...
3
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
1
by: Owen Blacker | last post by:
I've spent loads of time Googling to try to work this one out and I'm sure it's something obvious. I get an InvalidOperationException reading "Databinding methods such as Eval(), XPath(), and...
0
by: brian.newman | last post by:
I've created a dropdownlist whose values are drawn from an Access database. This list of items represents a user account. In the same record is a boolean field which records whether the user is...
2
by: cjard | last post by:
I ask, because I have a textbox bound to a bindingsource, which is bound to a datatable(row) The datatable is typed, and hence exports a Property called Column1 I added another property to the...
19
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.