Advertisement

07.23.2008 at 02:55PM PDT, ID: 23590342
[x]
Attachment Details

can this code be ran in a loop as a generic function?

Asked by savache27 in Microsoft Visual Basic.Net, Programming for ASP.NET

Tags: vb.net, sql server 2000, visual studio 2003

I have several questions that this code would work with on a page. Currently I'm trying to run it like this (code below). I was wondering if there is some way that I could set it up as a generic function and run different questions through it? Like some type of loop? Or do you think it will matter if I am doing it the way I currently am. There is a total of 13 questions on the screen and I could use the code for about 8 of them.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
Private Sub Race()
        q3 = "3. Race:"
        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(race) AS RaceCount, race FROM tbl_Alumni_Survey_data GROUP BY race"
        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"">" & q3 & "</FONT>"
        vHtml = vHtml & "</th>"
        vHtml = vHtml & "</tr>"
        vHtml &= "<tr bgcolor=""E5E5E5"">"
        Mycomm = New SqlCommand(sql, Myconn)
        dtr = Mycomm.ExecuteReader()
        Dim race As New ArrayList, RaceCount As New ArrayList
        Dim totalCount = 0, tempCount As Integer
        If dtr.HasRows Then
            While dtr.Read()
                race.Add(dtr("race")) 'Add new state to the list'
                tempCount = CInt(dtr("RaceCount")) 'Convert to an integer'
                RaceCount.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 rowCount = 0, i = 0, j As Integer = 0
            Do
                vHtml = vHtml & "<TD align=""left""><FONT face=""Verdana"" size=""1""></FONT></TD>"
                While Int(i / 8) = rowCount And i < RaceCount.Count
                    If race(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"">" & race(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 / 8) = rowCount And j < RaceCount.Count
 
                    vHtml &= "<TD align=""left"" width=""11%""><FONT face=""Verdana"" size=""1"" align=""center""> " & CInt(RaceCount(j) / totalCount * 100) & "% </FONT></TD>"
                    vHtml &= "</th>"
                    j += 1
                End While
                vHtml &= "</TR><TR>"
                rowCount += 1
                If j = RaceCount.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 & "&nbsp;</td>"
        vHtml = vHtml & "</tr>"
        vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
        vHtml = vHtml & "<td colspan=""11"" valign=""bottom"" align=""left"">"
        vHtml = vHtml & "&nbsp;</td>"
        vHtml = vHtml & "</tr>"
        vHtml = vHtml & "</table>"
        vHtml = vHtml & "<br/>"
    End Sub
    Private Sub CountState2()
        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"">"
        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 rowCount = 0, i = 0, j As Integer = 0
            Do
                vHtml = vHtml & "<TD align=""left""><FONT face=""Verdana"" size=""1""></FONT></TD>"
                While Int(i / 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
                While Int(j / 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>"
                rowCount += 1
                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 & "&nbsp;</td>"
        vHtml = vHtml & "</tr>"
        vHtml = vHtml & "<tr bgcolor=""#8B96B1"">"
        vHtml = vHtml & "<td colspan=""11"" valign=""bottom"" align=""left"">"
        vHtml = vHtml & "&nbsp;</td>"
        vHtml = vHtml & "</tr>"
        vHtml = vHtml & "</table>"
        vHtml = vHtml & "<br/>"
        lblResults.Text = vHtml
    End Sub
[+][-]07.23.2008 at 11:06PM PDT, ID: 22076408

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 07:20AM PDT, ID: 22079391

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 07:53AM PDT, ID: 22079817

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 08:17AM PDT, ID: 22080102

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Programming for ASP.NET
Tags: vb.net, sql server 2000, visual studio 2003
Sign Up Now!
Solution Provided By: alainbryden
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.24.2008 at 09:29AM PDT, ID: 22080944

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 09:50AM PDT, ID: 22081157

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 09:53AM PDT, ID: 22081183

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 11:18AM PDT, ID: 22081957

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 12:48PM PDT, ID: 22082819

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 02:55PM PDT, ID: 22083941

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628