Can't get a db value to evaluate to TRUE...why?

D

darrel

I've been struggling to get a repeater control set up to check a record
field, and, depending on what it is, render a different HTML template.

I've tried using a simple server-side span:

<span runat="server" Visible='<%# DataBinder.Eval(Container.DataItem,
"Application").ToString() = "Notice" %>'>
<custom html>
</span>

....and calling a function:

<%# TestSub(DataBinder.Eval(Container.DataItem, "Application"))%>

Function TestSub(ByVal YourItem as String)
If (YourItem="Notice") Then
Return "it works!"
Else
Return YourItem
End If
End Function

I've (FINALLY!) narrowed down the problem to the actual comparison. The same
thing happens with both methods...I can NOT get the data to evaluate to
TRUE. I've tried a number of things like:

<%# DataBinder.Eval(Container.DataItem, "Application") %>
= Notice

<%# DataBinder.Eval(Container.DataItem, "Application") = "Notice" %>
= False

<%# DataBinder.Eval(Container.DataItem, trim("Application")) =
trim("Application") %>
= False (this one REALLY has me stumped)

<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,
trim("Application")).ToString() = "Notice") %>
= False

So, it appears that I'm not comparing two identical things. And I'm not sure
what else to look at here. Is this an issue with SQL server, perhaps? Do I
need to caste these values as something completely different?
 
D

darrel

Maybe the text is "notice" instead of "Notice".

Not that I can tell. This is what is returned:
<%# DataBinder.Eval(Container.DataItem, "Application") %>
= Notice

with the uppercase Notice. Changing it to 'notice' in the comparison still
returns a false value.

-Darrel
 
M

Michael Ramey

I just set up a test, and it worked for me, can you post your entire
repeater tag/relavant functions just so we know exactly what you are trying
to do.

--Michael

btw here is what worked for me...working off of northwind db. I got many
bullets, but only text that showed up was "Fuller"

<asp:repeater id="Repeater1" runat="server">
<headertemplate><ul></headertemplate>
<itemtemplate><li><span runat="server" visible='<%#
DataBinder.Eval(Container.DataItem, "lastname") = "Fuller"%>'><%#
DataBinder.Eval(Container.DataItem, "lastname")%></span></li></itemtemplate>
<footertemplate></ul></footertemplate>
</asp:repeater>

Private conn As New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
conn.Open()

Dim sql As String
sql = "Select * from employees"

Dim cmd As New SqlCommand(sql, conn)
Dim dr As SqlDataReader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)

Repeater1.DataSource = dr
Repeater1.DataBind()
End Sub
 
D

darrel

I just set up a test, and it worked for me, can you post your entire
repeater tag/relavant functions just so we know exactly what you are trying
to do.

Sure...though if it works fine for you, I'm wondering if it's an issue with
the data object in our database?

Here's the entire page (it's not that big). Right now, I'm just trying to
get the template to return 'It Works!" if the data field = "Notice" (which,
AFAICT, it DOES, but still resolves to FALSE). Right now, when I run the
page, it doesn't equate the data field = Notice, so just returns the data
field, which shows up as (inexeplicably) 'Notice' (instead of 'It Works!')

===================================================================

<%@ Control Language="vb" %>

<script runat="server">
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strConnect As String
strConnect = ConfigurationSettings.AppSettings("DBConn")
Dim strChk As String
strChk = "Select top 10 Postid, postdate, Application, title FROM
WePostings order by Postdate DESC "

Try
'Dim objConnect As New OleDb.OleDbConnection(strConnect)
Dim objConnect As New System.Data.OleDb.OleDbConnection(strConnect)
objConnect.Open()
'Dim objCommand As New OleDb.OleDbCommand(strChk, objConnect)
Dim objCommand As New System.Data.OleDb.OleDbCommand(strChk, objConnect)
Repeater1.DataSource = objCommand.ExecuteReader()
Repeater1.DataBind()
Catch objError As Exception
' outMessage.InnerHtml = "Error accessing Database.<br />"
_
' & objError.Message & "<br />" & objError.Source
Exit Sub
End Try

End Sub

Function TestSub(ByVal YourItem as String)
If (YourItem="Notice") Then
Return "it works!"
Else
Return YourItem
End If
End Function

</script>


<form runat="server">
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>

<%# TestSub(DataBinder.Eval(Container.DataItem, "Application"))%>

</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</form>
 
M

Michael Ramey

still works for me, have you tried using the function
String.Compare(YourItem, "Notice") = true for your comparison?

You may want to specify a return type for your function, although I already
tested it and it didn't make a difference to me, still it's best to avoid
uneeded conversions in the background.

Function TestSub(ByVal YourItem as String) as String

Can you debug and see what is in each variable at runtime?

Also why are you using oledbconnection, oledbreaders, etc. I guess they'd
work, but sqlconnection/sqlreader/etc. are optimized for just sql server so
you should use them.

--Michael
 
D

darrel

still works for me,

So, would that indicate that something is goofy with the data coming from
the database? I'm not sure how, exactly, you are testing it. Are you getting
a TRUE return, or just that it does return 'NOTICE'?
have you tried using the function
String.Compare(YourItem, "Notice") = true for your comparison?

Let me try...

Function TestSub(ByVal YourItem as String)
If String.Compare(YourItem, "Notice") = true Then
Return "it's true!"
Else
Return "it's false!"
End If
End Function

returns... 'it's false'. Replacing that with Return YourItem returns
'Notice'...which just doesn't make any sense. *sigh*.

Note that even before I couldn't get a comparison of itself to itself to
return 'true'.
Function TestSub(ByVal YourItem as String) as String

Yep...same results for me.
Can you debug and see what is in each variable at runtime?

Umm...no. I'm actually avoiding VS.NET right now and doing most of this by
hand in DW. Is there a way to debug sans VS.NET?
Also why are you using oledbconnection, oledbreaders, etc. I guess they'd
work, but sqlconnection/sqlreader/etc. are optimized for just sql server so
you should use them.

Oh. Well, I'm just learning, so I'll have to go look at that! Thanks for the
tip.

Thanks for working on this, though. I'm still completely befuddled as to why
it won't work here on my end.

-Darrel
 
D

darrel

AAAAARRRRRGGGGGGGGGHHHHHHHHHHHHHHHH!

Michael:

As soon as I posted that last reply, I realized what was going on. The
repeat is checking only the first record. In otherwords, the first record
happens to *not* be a 'Notice* so it is returning a 'false'. Now, to figure
out how to get it to check each individual record instead of just the first
one...

-Darrel
 
D

darrel

Gah. I have NO IDEA how I finally got to this, but this works!:

If (trim(YourItem)="Notice") Then
Return "it works!"

so...I think this means that the database is storing more than just 'Notice'
but is including blank spaces?

-Darrel
 
M

Michael Ramey

mmm, spaces in the fields, now that problem has thrown me off many of times,
especially when you import from text files, if you don't watch it sometimes
it'll throw spaces in the fields.
 

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
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top