Web Services
You then create two datasets, and create some variables to help you keep track of where you are in the datasets:
Dim ds As New DataSet
Dim ds2 As New DataSet
Dim position As Integer
Dim offset As Integer
Dim rows As DataRowCollection
Next you fill the first adapter with the results of the first SQL query in sqlstring1, which returns the whole league table in order. You then iterate through this table, position by position, until you find an entry that matches “Wrox United”. You save that position into the variable position:
For i As Integer = 0 To rows.Count - 1
If rows(i)(“Name”).ToString() = “Wrox United” Then position = i + 1
End If
Next
You have to cater for three scenarios here:
The golden scenario: Wrox United is top, and you need to display the second and third place teams.
The doomsday scenario: Wrox United is bottom and you need to display the two teams above them.
The usual scenario: Wrox United is neither top nor bottom, but somewhere in between, in which case you display one team above them and one team below them.
Your first if condition deals with the case where the position isn’t top or bottom of the league. You take two off the position (so if Wrox United is 5, this would be 3). Then you fill the adapter2 with the adapter2 starting at position 3, and the number 3 indicates that there are three teams only. For example:
Adapter2.Fill(name of dataset, position to start in dataset, number of rows, name of query).
So you say if the position isn’t 1 and isn’t last (you obtain the amount for last by counting the number of teams in the league), then you set the offset to the position minus 2 and pass that to the adapter2.Fill method at the end of the if then condition:
If position > 1 And position < rows.Count Then offset = position - 2
Of course if Wrox United has come in fifth, why are you starting with third? Surely that would display third, fourth, and fifth, when actually you intend to display fourth, fifth, and sixth? The answer is that when you filled your adapter, you started at row 0. So the team that came first is 0, the team that came second is 1, and so on. So the preceding line to fill the adapter actually does return only three teams, and will return the teams in fourth, fifth, and sixth.