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.
I fail to see the need for all the
ifs If you do a: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.