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

Discussion in 'ASP .Net' started by darrel, Feb 10, 2004.

  1. darrel

    darrel Guest

    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?
    darrel, Feb 10, 2004
    #1
    1. Advertising

  2. darrel

    darrel Guest

    > 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
    darrel, Feb 10, 2004
    #2
    1. Advertising

  3. 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

    "darrel" <> wrote in message
    news:...
    > 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?
    >
    >
    Michael Ramey, Feb 10, 2004
    #3
  4. darrel

    darrel Guest

    > 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>
    darrel, Feb 10, 2004
    #4
  5. 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


    "darrel" <> wrote in message
    news:...
    > > 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>
    >
    >
    Michael Ramey, Feb 10, 2004
    #5
  6. darrel

    darrel Guest

    > 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
    darrel, Feb 10, 2004
    #6
  7. darrel

    darrel Guest

    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
    darrel, Feb 10, 2004
    #7
  8. darrel

    darrel Guest

    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
    darrel, Feb 10, 2004
    #8
  9. 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.

    "darrel" <> wrote in message
    news:...
    > 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
    >
    >
    Michael Ramey, Feb 11, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark A. Odell

    NULL: Is it guaranteed to evaluate 'not true'

    Mark A. Odell, Sep 25, 2003, in forum: C Programming
    Replies:
    19
    Views:
    464
    Dan Pop
    Sep 30, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  3. Paminu

    Which numbers evaluate to true and false?

    Paminu, Oct 4, 2005, in forum: C Programming
    Replies:
    22
    Views:
    805
    August Karlstrom
    Oct 6, 2005
  4. bdb112
    Replies:
    45
    Views:
    1,312
    jazbees
    Apr 29, 2009
  5. Steve
    Replies:
    5
    Views:
    155
Loading...

Share This Page