locked
only the first record to come in datareader problem RRS feed

  • Question

  • User1269363623 posted

    first my english is not very good. I hope I can tell my trouble.
    I am trying to tag of some words.There are 5 tag word in my database. but only the first record is coming.
    i want to

    tags: bbbla bllab blalb abll ablla
    Where am I doing wrong. thanks
    my codes
     Public Function tags(ByVal ID As Integer) As String

            Dim com As OleDbCommand = conn.ExecCommand("select tag from tag where ID = @p1")
            com.Parameters.Add("@p1", OleDbType.Variant).Value = 99
            conn.connectionopen()
            Dim reader As OleDbDataReader = al.ExecuteReader
            While reader.Read

                Return "<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |"

            End While
            conn.connectionclose()
            reader.Close()


        End Function

    my html code in gridview <%#tags(Container.DataItem("ID"))%>

    Monday, September 21, 2009 10:30 AM

Answers

  • User-952121411 posted

            While reader.Read

                Return "<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |"

            End While
     

     

    Yeah the 'Return' statement will exit execution as soon as it is hit, so the 'While' loop going through your reader will not get to the next iteration and your subsiquient records.

    Maybe try using a StringBuilder or something similar to concatenate your string together and then return that value.

    Dim reader As OleDbDataReader = al.ExecuteReader
    Dim sb As New System.Text.StringBuilder
    While reader.Read
    
                sb.Append("<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |")
    
    End While
    
    
    Return sb.ToString.Trim()


    ...Of corse you may not want one big concatenated string, so you could place each string into a string array or any object type you want.  The point is, you will want to allow that Reader to continue looping all the way through before executing your 'Return' statement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2009 12:34 PM

All replies

  • User187056398 posted

     Maybe it's because you return immediately after reading one record.

    Return "<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |"

    Monday, September 21, 2009 10:43 AM
  • User-952121411 posted

            While reader.Read

                Return "<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |"

            End While
     

     

    Yeah the 'Return' statement will exit execution as soon as it is hit, so the 'While' loop going through your reader will not get to the next iteration and your subsiquient records.

    Maybe try using a StringBuilder or something similar to concatenate your string together and then return that value.

    Dim reader As OleDbDataReader = al.ExecuteReader
    Dim sb As New System.Text.StringBuilder
    While reader.Read
    
                sb.Append("<a href=?tag=" + reader.Item("tag") + ">" + reader.Item("tag") + "</a> |")
    
    End While
    
    
    Return sb.ToString.Trim()


    ...Of corse you may not want one big concatenated string, so you could place each string into a string array or any object type you want.  The point is, you will want to allow that Reader to continue looping all the way through before executing your 'Return' statement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2009 12:34 PM