Private Sub CountState()
Dim vHtml, q4, numrec
q4 = "4. State of Current Residence:"
Dim Myconn As SqlConnection
Dim Mycomm As SqlCommand
Dim sql As String
Dim dtr As SqlDataReader
Myconn = New SqlConnection(ConfigurationSettings.AppSettings("gDataSource"))
Myconn.Open()
sql = "SELECT DISTINCT Count(state) AS StateCount, state FROM tbl_Alumni_Survey_data GROUP BY state"
vHtml = vHtml & "<table cellpadding=""3"" cellspacing=""1"" width=""100%"" align=""center"">"
vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
vHtml = vHtml & "<th colspan=""9"" valign=""bottom"" width=""25%"" align=""left"">"
vHtml = vHtml & "<FONT face=""Verdana"" size=""2"" align=""center"">" & q4 & "</FONT>"
vHtml = vHtml & "</th>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""E5E5E5"">"
Mycomm = New SqlCommand(sql, Myconn)
dtr = Mycomm.ExecuteReader()
If dtr.HasRows Then
While dtr.Read()
vHtml = vHtml & "<TD align=""left""><FONT face=""Verdana"" size=""1"">'" & dtr("state") & "'</FONT></TD>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""#B9C0D0"">"
vHtml = vHtml & "<th valign=""bottom"" width=""11%"" align=""left"">"
vHtml = vHtml & "<FONT face=""Verdana"" size=""1"" align=""center"">'" & dtr("StateCount") & "'</FONT>"
vHtml = vHtml & "</th>"
End While
End If
dtr.Close()
Myconn.Close()
lblResults.Text = vHtml
End Sub
Dim states as new ArrayList(), stateCounts as new ArrayList()
Dim totalCount = 0, tempCount as Integer
...
If dtr.HasRows Then
While dtr.Read()
states.Add(dtr("state")) 'Add new state to the list'
tempCount = cInt(dtr("StateCount")) 'Convert to an integer'
stateCounts.Add(tempCount) 'Add new count to the list'
totalCount += tempCount 'Add count to the total'
End While
'Now we can display the statistics'
Dim done as boolean = false
Dim i = 0, j as Integer = 0
Do
While i+1 Mod 8 <> 0 And i < states.Count
vHtml &= "<TD> " & states(i) & " </TD>"
i += 1
Loop
vHtml &= "</TR><TR>"
While j+1 Mod 8 <> 0 And j < stateCounts.Count
vHtml &= "<TD> " & cInt(stateCounts(j) / totalCount * 100) & "% </TD>"
j += 1
Loop
vHtml &= "</TR><TR>"
If j = stateCounts.Count then done = True 'Check if we are done'
Loop While done = False
End If
Private Sub CountState2()
Dim vHtml, q4, numrec
q4 = "4. State of Current Residence:"
Dim Myconn As SqlConnection
Dim Mycomm As SqlCommand
Dim sql As String
Dim dtr As SqlDataReader
Myconn = New SqlConnection(ConfigurationSettings.AppSettings("gDataSource"))
Myconn.Open()
sql = "SELECT DISTINCT Count(state) AS StateCount, state FROM tbl_Alumni_Survey_data GROUP BY state"
vHtml = vHtml & "<table cellpadding=""3"" cellspacing=""1"" width=""100%"" align=""center"">"
vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
vHtml = vHtml & "<th colspan=""10"" valign=""bottom"" align=""left"">"
vHtml = vHtml & "<FONT face=""Verdana"" size=""2"" align=""center"">" & q4 & "</FONT>"
vHtml = vHtml & "</th>"
vHtml = vHtml & "</tr>"
Mycomm = New SqlCommand(sql, Myconn)
dtr = Mycomm.ExecuteReader()
Dim states As New ArrayList, stateCounts As New ArrayList
Dim totalCount = 0, tempCount As Integer
If dtr.HasRows Then
While dtr.Read()
states.Add(dtr("state")) 'Add new state to the list'
tempCount = CInt(dtr("StateCount")) 'Convert to an integer'
stateCounts.Add(tempCount) 'Add new count to the list'
totalCount += tempCount 'Add count to the total'
End While
'Now we can display the statistics'
Dim done As Boolean = False
Dim i = 0, j As Integer = 0
Do
While i + 1 Mod 8 <> 0 And i < states.Count
'vHtml &= "<tr bgcolor=""E5E5E5"">"
vHtml &= "<TD align=""left"" bgcolor=""E5E5E5""><FONT face=""Verdana"" size=""1"">" & states(i) & " </FONT></TD>"
'vHtml &= "<TD> " & states(i) & " </TD>"
i += 1
End While
vHtml &= "</TR><TR>"
While j + 1 Mod 8 <> 0 And j < stateCounts.Count
vHtml &= "<TD align=""left"" bgcolor=""#B9C0D0""><FONT face=""Verdana"" size=""1"" align=""center""> " & CInt(stateCounts(j) / totalCount * 100) & "% </FONT></TD>"
'vHtml &= "<TD> " & CInt(stateCounts(j) / totalCount * 100) & "% </TD>"
j += 1
End While
vHtml &= "</TR><TR>"
If j = stateCounts.Count Then done = True 'Check if we are done'
Loop While done = False
End If
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""#E5E5E5"">"
vHtml = vHtml & "<td colspan=""10"" valign=""bottom"" align=""left"">"
vHtml = vHtml & " </td>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
vHtml = vHtml & "<td colspan=""10"" valign=""bottom"" align=""left"">"
vHtml = vHtml & " </td>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "</table>"
lblResults.Text = vHtml
End Sub
Dim q4
q4 = "4. State of Current Residence:"
Dim Myconn As SqlConnection
Dim Mycomm As SqlCommand
Dim sql As String
Dim dtr As SqlDataReader
Myconn = New SqlConnection(ConfigurationSettings.AppSettings("gDataSource"))
Myconn.Open()
sql = "SELECT DISTINCT Count(state) AS StateCount, state FROM tbl_Alumni_Survey_data GROUP BY state"
vHtml = vHtml & "<table cellpadding=""3"" cellspacing=""1"" width=""100%"" align=""center"">"
vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
vHtml = vHtml & "<th colspan=""11"" valign=""bottom"" align=""left"">"
vHtml = vHtml & "<FONT face=""Verdana"" size=""2"" align=""center"">" & q4 & "</FONT>"
vHtml = vHtml & "</th>"
vHtml = vHtml & "</tr>"
vHtml &= "<tr bgcolor=""E5E5E5"">"
vHtml = vHtml & "<TD align=""left""><FONT face=""Verdana"" size=""1""></FONT></TD>"
Mycomm = New SqlCommand(sql, Myconn)
dtr = Mycomm.ExecuteReader()
Dim states As New ArrayList, stateCounts As New ArrayList
Dim totalCount = 0, tempCount As Integer
If dtr.HasRows Then
While dtr.Read()
states.Add(dtr("state")) 'Add new state to the list'
tempCount = CInt(dtr("StateCount")) 'Convert to an integer'
stateCounts.Add(tempCount) 'Add new count to the list'
totalCount += tempCount 'Add count to the total'
End While
'display the statistics
Dim done As Boolean = False
Dim i = 0, j As Integer = 0
Do
While (i + 1) Mod 8 <> 0 And i < states.Count
'vHtml &= "<tr bgcolor=""E5E5E5"">"
If states(i) = "" Then
vHtml &= "<TD align=""left""><FONT face=""Verdana"" width=""11%"" size=""1"">Omit</FONT></>"
Else
vHtml &= "<TD align=""left""><FONT face=""Verdana"" width=""11%"" size=""1"">" & states(i) & " </FONT></TD>"
End If
i += 1
End While
vHtml &= "</TR><TR bgcolor=""#B9C0D0"">"
vHtml = vHtml & "<TD align=""left"" width=""11%""><FONT face=""Verdana"" size=""1""></FONT></TD>"
While (j + 1) Mod 8 <> 0 And j < stateCounts.Count
vHtml &= "<TD align=""left"" width=""11%""><FONT face=""Verdana"" size=""1"" align=""center""> " & CInt(stateCounts(j) / totalCount * 100) & "% </FONT></TD>"
vHtml = vHtml & "</th>"
'vHtml &= "<TD> " & CInt(stateCounts(j) / totalCount * 100) & "% </TD>"
j += 1
End While
vHtml &= "</TR><TR>"
If j = stateCounts.Count Then done = True 'Check if we are done'
Loop While done = False
End If
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""#E5E5E5"">"
vHtml = vHtml & "<td colspan=""11"" valign=""bottom"" align=""left"">"
vHtml = vHtml & " </td>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
vHtml = vHtml & "<td colspan=""11"" valign=""bottom"" align=""left"">"
vHtml = vHtml & " </td>"
vHtml = vHtml & "</tr>"
vHtml = vHtml & "</table>"
vHtml = vHtml & "<br/>"
lblResults.Text = vHtml
'display the statistics'
Dim done As Boolean = False
Dim rowCount = 0, i = 0, j As Integer = 0
Do
While Int((i + 1) / 8) = rowCount And i < states.Count
If states(i) = "" Then
vHtml &= "<TD align=""left""><FONT face=""Verdana"" width=""11%"" size=""1"">Omit</FONT></>"
Else
vHtml &= "<TD align=""left""><FONT face=""Verdana"" width=""11%"" size=""1"">" & states(i) & " </FONT></TD>"
End If
i += 1
End While
vHtml &= "</TR><TR bgcolor=""#B9C0D0"">"
vHtml &= "<TD align=""left"" width=""11%""><FONT face=""Verdana"" size=""1""></FONT></TD>"
While Int((j + 1) / 8) = rowCount And j < stateCounts.Count
vHtml &= "<TD align=""left"" width=""11%""><FONT face=""Verdana"" size=""1"" align=""center""> " & CInt(stateCounts(j) / totalCount * 100) & "% </FONT></TD>"
vHtml &= "</th>"
j += 1
End While
vHtml &= "</TR><TR>"
rRowCount = rowCount + 1
If j = stateCounts.Count Then done = True 'Check if we are done'
Loop While done = False
To do this, since you are using an SQL reader, you will have to first read everything into a 2D array, and then generate the HTML in the required order.
To get the percentage simple just add a % sign after you've output the state count:
...align=""center"">'" & dtr("StateCount") & "%'"
Also, if you don't want the single quotes appearing, remove the single quotes right before you close and right after you open your double quotes before outputting a variable, although I'm sure you knew that.
Once all this is worked out, you can add a counter to your loop. Every time the counter MOD 8 equals 0, you can start a new row.
So it would look like this:
1. ExecuteReader
2. In a loop, save all states and statecounts in a 2D array
3. Initialize i = 0, j = 0
3. Start a loop that goes until all your entries have been output
4. Start a while loop, while i+1 MOD 8 != 0
5. Display state i
6. i ++
7. Start a new table row ()
8 Start a while loop, while j+1 MOD 8 != 0
9. Display statecount j
10. j++
11. Start a new table row
12. End the loop if you're done displaying everything
You can check if you've finished displaying everything with a boolean variable.
Also make sure that you're checking your index bounds in each of the internal loops. Neither i nor j should ever exceed the number of states you have. Once they do, set your boolean variable indicating that you are done to true, and exit the while loop.