Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:17:18+00:00 2026-05-26T17:17:18+00:00

I am trying to pull the top three rows from a database based off

  • 0

I am trying to pull the top three rows from a database based off of the highest count and then assign those fields to the appropriate properties class object that I then pass to the form were they are displayed. I am mainly having trouble telling the top positions by count (1,2,3) to be assigned to the correct object. Right now I am only testing with two objects. Any help would be great, thanks in advance

Here is the code for the function I am working on:

 ' Function to get the top 3 articles by count from the database and assign them to an object that is then passes to the application form
' This is the main function that I have been working with to accomplish passing the articles. As of now it is called after the application form is loaded
Function fncGetArticles() As Boolean
    'Declares needed objects for the function
    Dim myReader1 As MySqlDataReader
    Dim objArticleInfo1 As New clsArticleProperties
    Dim objArticleInfo2 As New clsArticleProperties
    Dim obtain1 As New MySqlCommand


    'Defines connection and SQL statement that selects the top three articles ordered by count
    obtain1.CommandText = "SELECT * FROM Articles ORDER BY Count DESC LIMIT 3"
    obtain1.CommandType = CommandType.Text
    obtain1.Connection = connection
    connection.Open()

    myReader1 = obtain1.ExecuteReader

    While myReader1.Read()

        'these blocks of code assign the field from the database to an object that is then passed to the application form
        'I need a way to tell it to pass the correct fields based off of there count but have been unable to find a way to do this
        'I have experimented a lot with IF...THEN statements for this but have not gotten any thing to work


        'I basically need a better way to assign the fields base off of their order to the designated objects

        'The code after the IF is my way of saying "If the article was 1st in the order then assign it these properties"
        'If myReader1.GetOrdinal("Count") = "SELECT MAX(Count) FROM Articles" Then
        objArticleInfo1.Username = myReader1.GetValue(myReader1.GetOrdinal("Submitby"))
        objArticleInfo1.URL = myReader1.GetValue(myReader1.GetOrdinal("URL"))
        objArticleInfo1.Title = myReader1.GetValue(myReader1.GetOrdinal("Title"))
        objArticleInfo1.SubmitDate = myReader1.GetValue(myReader1.GetOrdinal("Date"))
        objArticleInfo1.Count = myReader1.GetValue(myReader1.GetOrdinal("Count"))
        objArticleInfo1.Comments = myReader1.GetValue(myReader1.GetOrdinal("Comments"))
        objArticleInfo1.Category = myReader1.GetValue(myReader1.GetOrdinal("Category"))
        'End If

        'The code after the IF here is my way of assigning the data from the 2nd spot in the order to the designated object
        'If myReader1.GetOrdinal("Count") < "SELECT MAX(Count) FROM Articles AND Count > SELECT MIN(Count) FROM Articles" Then
        objArticleInfo2.Username = myReader1.GetValue(myReader1.GetOrdinal("Submitby"))
        objArticleInfo2.URL = myReader1.GetValue(myReader1.GetOrdinal("URL"))
        objArticleInfo2.Title = myReader1.GetValue(myReader1.GetOrdinal("Title"))
        objArticleInfo2.SubmitDate = myReader1.GetValue(myReader1.GetOrdinal("Date"))
        objArticleInfo2.Count = myReader1.GetValue(myReader1.GetOrdinal("Count"))
        objArticleInfo2.Comments = myReader1.GetValue(myReader1.GetOrdinal("Comments"))
        objArticleInfo2.Category = myReader1.GetValue(myReader1.GetOrdinal("Category"))
        'End If
        myReader1.Close()

        frmApplicationWindow.passarticles1(objArticleInfo1, objArticleInfo2)

        connection.Close()
    End While
    Return Nothing
End Function

Update: I just received this answer from someone else but I really don’t know exactly what they are trying to tell me. Any ideas????

In the form where you are displaying this information, consider how you are calling the function:

objDB.getTopArticles(1, objArt1)

where objArt1 is an empty version of the article object.

Function getTopArticles accepts these parameters:

Function getTopArticles(ByVal rankRetrieved As Integer, ByRef artObject As classArticles)

You can then ask for the ‘correct’ article by extending your SQL statement that you have, offsetting the request by rankRetrieved – 1 to make sure you line up with the 0 spot in the DB array.

You would run this three times (from the form) for the three different articles.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:17:18+00:00Added an answer on May 26, 2026 at 5:17 pm

    I fail to see the need for all the ifs If you do a:

    SELECT * FROM Articles ORDER BY Count DESC LIMIT 3
    

    The first row will be the highest count, the last row will have the lowest count (of the three)
    Just fetch the rows in order and you’re done.

    Also don’t do select *, it’s an anti-pattern, only fetch the rows you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to pull excerpts and custom fields from specific posts, I have
Trying to pull rows from a table and list them in an ol, however,
I am trying to pull at list of resource/database names and IDs from a
Trying to pull fields 'info' and 'date' from Comment table. Two Tables: Php <?php
I'm trying to get a sliding drawer to pull down from the top in
I've been trying to pull a copy of my sqlite database from my android
I'm trying to pull in data from a remote SQL Server. I can access
I am trying to pull registry info from many servers and put them all
I am trying to pull information from another site. When I try and do
I am trying to pull out data from the table I had from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.