I am having a problem with my datalist and repeaters. Below is the code. For some reason, when it searches the database and returns results, it counts the correct number of results, but only displays the correct number minus one. So if it returns 3 results, it only displays 2, returns 9, displays 8. This is true for the first two repeaters bound on the first SQL query. The second query counts the right number of records, but doesn't bind to the control like it is supposed to! I get no errors, just no records! Thank you.

if Not Page.IsPostBack then
  if Not Session("UAID") Is Nothing then
    Dim conPubs As New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
    Dim dtrReader As OdbcDataReader
    Dim strUAID As String = Session("UAID")
    Dim intOnlineFriends As Integer = 0
    Dim cmdSelect As New OdbcCommand( "SELECT b.*, c.*, a.Views, a.MainPic, a.UserName, (SELECT COUNT(CommentID) FROM ProfileComments WHERE ReceiveUserID=? and WasRead=0) As intComments, (SELECT COUNT(MessageID) FROM Messages WHERE ReceiveUserID=? and WasRead=0) As intMessages FROM Users a LEFT JOIN ProfileComments b ON b.ReceiveUserID=a.UserID LEFT JOIN Messages c ON c.ReceiveUserID=a.UserID WHERE a.UserID=? LIMIT 5", conPubs )
    cmdSelect.Parameters.AddWithValue( "?ReceiveUserID", strUAID )
    cmdSelect.Parameters.AddWithValue( "?ReceiveUserID", strUAID )
    cmdSelect.Parameters.AddWithValue( "?UserID", strUAID )
    Dim cmdSelect2 As New OdbcCommand( "SELECT (SELECT COUNT(UserID) As intOnlineFriends FROM Users WHERE Logged=1 AND UserID IN (SELECT FriendID FROM ProfileFriends WHERE UserID=?)) As intOnlineFriends, MainPic, UserID FROM Users WHERE Logged=1 AND UserID IN (SELECT FriendID FROM ProfileFriends WHERE UserID=?) LIMIT 12", conPubs )
    cmdSelect2.Parameters.AddWithValue( "?UserID", strUAID )
    cmdSelect2.Parameters.AddWithValue( "?UserID", strUAID )
    conPubs.Open()
    dtrReader = cmdSelect.ExecuteReader()
    if dtrReader.HasRows then
      while dtrReader.Read()
        ltlUserName.Text = dtrReader("UserName")
        ltlMainPic.Text = "<img src=""/Profiles/" & Session("UAID") & "/" & dtrReader("MainPic") & """ alt="""""" />"
        if dtrReader("intComments") > 0 then ltlComments.Text = dtrReader("intComments") & " New Comments!"
        if dtrReader("intMessages") > 0 then ltlMessages.Text = dtrReader("intMessages") & " New Messages!"
        ltlViews.Text = FormatNumber(dtrReader("Views"),0)
        rpComments.DataSource = dtrReader
        rpComments.DataBind()
        rpMessages.DataSource = dtrReader
        rpMessages.DataBind()
      end while
      dtrReader.Close()
      dtrReader = cmdSelect2.ExecuteReader()
      if dtrReader.HasRows then
        while dtrReader.Read()
          ltlOnlineFriends.Text = dtrReader("intOnlineFriends") & " Friends are Online Now!"
        end while
        dlOnlineFriends.DataSource = dtrReader
        dlOnlineFriends.DataBind()
      end if
    end if
    dtrReader.Close()
    conPubs.Close()
    
    if conPubs.State = ConnectionState.Open then conPubs.Close()    
  else
    response.Redirect("/home.aspx")
  end if
end if

Oh and if you have the expertise to combine these two queries into one, that would be greatly appreciated as I am not as fluent with queries as I am with other programming. Keep in mind, they have different limits (query 1 has limit of 5, query 2 has limit of 12).

And any opinions on how to make the code better or more efficient, feel free to criticize. Thank you.

Nevermind, after walking away and thinking about it, I realized that I made a big mistake. I read all the data, then bound it. The reason why one row was always missing is that during the while read segment, I bound the data eternally. And the reason why the other repeater wasn't showing anything is that I bound the data after reading through the entire datareader. So it bound the left overs, which was nothing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.