steve
steve

Reputation: 93

why does this NOT work for me? it always returns TRUE

I have been pulling my hair out trying to figure out why this seems to work for others but not me.

I have some data and in the Hours2 column there are clearly cells with NULL and some with hours like this

'9:00 a.m. - 6:00 p.m.'
NULL
NULL
'9:00 a.m. - 6:00 p.m.'
'9:00 a.m. - 5:00 p.m.'
NULL
NULL
NULL

yet I can't figure out how to do what seems like if should be a simple IF statement with some inline code. I already use

<%# Eval("Hours2") %> to get the values and that shows up fine...

IN the first case I want to display one or the other bit of text if Hours2 is NULL or not. (this is the label text for my hours one data, if there is Hours2 then Friday has a different time)

<%# Eval("Hours2") != DBNull.Value ? "mon - thurs" : "mon - fri" %>

This based on answers from other on other things here at Stack sounds like it should work, but it's ALWAYS TRUE...

same thing if i'm trying to just use True or False to make things Visible or not...

Visible ='<%# Eval("Hours2") != DBNull.Value %>'

I'm clearly missing some thing here...!!!

Upvotes: 0

Views: 241

Answers (4)

Ashfaq Shaikh
Ashfaq Shaikh

Reputation: 1668

how you can get it from SQL. Means using LINQ or call SP? You can use isnull(Hours2,0) Hours2 or isnull(Hours2,'') Hours2 in your Query. and it will work for you like this.

<%# Eval("Hours2") != 0 ? "mon - thurs" : "mon - fri" %>

OR

<%# Eval("Hours2") != '' ? "mon - thurs" : "mon - fri" %>

OR

<%# !String.IsNullOrEmpty(Eval("Hours2").ToString()) ? "mon - thurs" : "mon - fri" %>

Upvotes: 0

Greg
Greg

Reputation: 8784

try this:

Visible ='<%# Eval("Hours2").ToString() != "" %>'

or

Visible ='<%# !String.IsNullOrEmpty(Eval("Hours2").ToString()) %>'

Upvotes: 0

Malk
Malk

Reputation: 11983

Move the logic to the codehind:

<%# GetString(Eval("Hours2")) %>

Then use your debugger to figure out what is actually being passed.

public string GetString(object hours){
 ...
}

Upvotes: 2

Esteban
Esteban

Reputation: 2513

You could try checking it a different way... this is how I might do it.

<%#(String.IsNullOrEmpty(Eval("Hours2").ToString()) ? "mon-thurs" : "mon-fri")%>

Upvotes: 0

Related Questions