I'm working on a project, at work, called the "Bus Driver." The bus driver is a material handler that goes to 9 different assembly lines, on the production floor, and picks up finished goods (refurbished receivers) and drops them off at a converyer belt where they are then shrinked wrapped and sent to be palletized.
The application I'm working on tracks each time he makes one complete route and then stores his stats in an SQL DB.
The application that records his route stats works fine but I have another application called, "The Bus Driver Analyzer." This application gets all the Bus driver's data and produces 4 charts.
- Chart 1: Route Efficiency (52 weeks are shown, in this chart, on the X-value)
- Chart 2: On time delivery Rates (52 weeks are shown, in this chart, on the X-value)
- Chart 3: Histogram Chart (there is a radio button for a YTD or Weekly option)
- Chart 4: Stack Ranking chart (top 10)
The two charts below are the two charts i want to focus on for this question.
My problem with this application is that I have 52 labels, on the windows form, to hold his route efficiencies for each week (seen on your left.) They are called "LblWkEff1" and goes up to "LblWkEff52" and then I have another 52 labels, on the windows form to hold his On time delivery rates (Seen on your right.) They are called "lblDeliveryStat1" and go up to "lblDeliveryStat52."
The code I have to retrieve these results is horrendeous, from what i've been told, on this site, and I don't disagree. I'm still new to programming so what I have written is by no means clean and perfect.
here is my code:
Dim RESULT1 As Decimal 'declare this as global
Dim RESULT2 As Decimal 'declare this as global
Private Sub Week(ByVal week As Integer)
Dim queryString As String = "SELECT " & _
" (SELECT CAST(SUM(TARGET_SECONDS) AS DECIMAL)/ CAST(SUM(ROUTE_SECONDS) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN WITH(NOLOCK) WHERE WEEK_TIME = " & week & " AND APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' AND YEAR_TIME = '" & cbYear.Text & "' AND ACTIVE = 1) AS RESULT1," & _
" (SELECT (SELECT CAST(COUNT(APE_BUSDRIVER_STATUS_OBJID) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN AS RESULT2 WHERE WEEK_TIME = " & week & " AND APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' AND YEAR_TIME = '" & cbYear.Text & "' AND ACTIVE = 1 AND APE_BUSDRIVER_STATUS_OBJID = 1)/(SELECT CAST(COUNT(APE_BUSDRIVER_STATUS_OBJID) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN AS RESULT2 WHERE WEEK_TIME = " & week & " AND APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' AND YEAR_TIME = '" & cbYear.Text & "' AND ACTIVE = 1)) AS RESULT2" & _
" FROM dbo.APE_BUSDRIVER_MAIN "
Using connection As New SqlConnection(SQLConnectionStr)
Dim command As New SqlCommand(queryString, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
' Call Read before accessing data.
If reader.HasRows Then
While reader.Read()
RESULT1 = reader("RESULT1")
RESULT2 = reader("RESULT2")
End While
Else
RESULT1 = 0
RESULT2 = 0
End If
' Call Close when done reading.
reader.Close()
End Using
End Sub Private Sub LoadWeeklyStats()
'LOOP AND QUERY
For i As Integer = 0 To 51
Week(i + 1)
Dim LabelWkEff As String = "LblWkEff" + (i + 1).ToString
Dim myArray1 As Array = Controls.Find(LabelWkEff, False)
Dim myControl1 As Label = myArray1(0)
myControl1.Text = RESULT1
'AND
Dim LabelDeliveryStat As String = "lblDeliveryStat" + (i + 1).ToString
Dim myArray2 As Array = Controls.Find(LabelDeliveryStat, False)
Dim myControl2 As Label = myArray2(0)
myControl2.Text = RESULT2
Next
End Sub
Private Sub LoadWeeklyStats()
is what im using to hold the results and place them on the "LblWkEffXX" and "lblDeliveryStatXX" labels, on the windows form.
this process takes 5 seconds each time a new user is selected for reviewal and I know it has something to do with the SQL query and the for loop but I dont know how else to write the code to get the results I want more efficiently.
Any feedback on how to rewrite the code or what other option i can perform to achieve the same results in a much quicker time would be most appreciated.
please let me know if you need more information.