PC Review


Reply
Thread Tools Rate Thread

DataBinding boolean values from a database

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      29th Aug 2008
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 Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Ken Cox
Guest
Posts: n/a
 
      29th Aug 2008
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>

"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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 Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
John Bell
Guest
Posts: n/a
 
      29th Aug 2008


"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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 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


 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      29th Aug 2008
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
(E-Mail Removed)
http://www.nathansokalski.com/

"John Bell" <(E-Mail Removed)> wrote in message
news:2A533FCC-6224-4C03-B791-(E-Mail Removed)...
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Boolean values to bytes Jay Dee Microsoft C# .NET 1 4th May 2009 01:34 AM
Using Boolean Values Paul Microsoft ASP .NET 0 20th Apr 2006 01:41 PM
ASP databinding DataFormatstring boolean =?Utf-8?B?SmFj?= Microsoft C# .NET 1 22nd Jul 2004 11:32 AM
Boolean Values TC Microsoft Access Database Table Design 4 29th Oct 2003 02:52 PM
boolean databinding Tom Microsoft VB .NET 1 20th Oct 2003 03:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:05 PM.