For counter = 0 To (ImpArray.Length - 1)
If IsNumeric(ImpArray(counter)) Then
'GET NUMBER OF RISKS
Dim strSQL1 = "SELECT _SAFETY_Risks.*, _EMS_Reg_Impacts.IMPACT AS HAZARDDESC " & _
"FROM _SAFETY_Risks INNER JOIN " & _
"_EMS_Reg_Impacts ON _SAFETY_Risks.HAZARD = _EMS_Reg_Impacts.ID " & _
"WHERE (_SAFETY_Risks.REC_COUNT = 0) AND (_EMS_Reg_Impacts.REC_COUNT = 0) AND ASPECTID = '" & lstItems.SelectedItem.Value & "' AND _EMS_Reg_Impacts.ID = '" & ImpArray(counter) & "'"
Dim dsNumRisks As New DataSet
dsNumRisks = currCC.executeQuery(Application("ConnString"), strSQL1, "PPE")
'Create Data Row
Dim rNumRisks As DataRow
For Each rNumRisks In dsNumRisks.Tables("PPE").Rows
txtNumRisks.Text = CInt(txtNumRisks.Text) + 1
Next
If txtNumImpacts.Text = "" Then
txtNumImpacts.Text = "1"
Else
txtNumImpacts.Text = CInt(txtNumImpacts.Text) + 1
End If
End If
Next
lstHazards.Items.Clear()
Dim initItem As New System.Web.UI.WebControls.ListItem
initItem.Value = "0"
initItem.Text = "-- Not Selected --"
lstHazards.Items.Add(initItem)
For counter = 0 To (ImpArray.Length - 1)
'Response.Write(ImpArray(counter) & "<br>")
If IsNumeric(ImpArray(counter)) Then
'==============Get Impact Name==================
strSQL = "SELECT * FROM _EMS_Reg_Impacts WHERE ID = '" & ImpArray(counter) & "'"
Dim dsImpName As New DataSet
dsImpName = currCC.executeQuery(Application("ConnString"), strSQL, "ImpName")
Dim rImpName As DataRow
If dsImpName.Tables("ImpName").Rows.Count > 0 Then
rImpName = dsImpName.Tables("ImpName").Rows(0)
'============HAZARDS DROP-DOWN=============
Dim currItem As New System.Web.UI.WebControls.ListItem
currItem.Value = ImpArray(counter)
currItem.Text = rImpName("IMPACT")
lstHazards.Items.Add(currItem)
'=========================================
End If
'GET RISKS
Dim strSQL1 = "SELECT _SAFETY_Risks.*, _EMS_Reg_Impacts.IMPACT AS HAZARDDESC " & _
"FROM _SAFETY_Risks INNER JOIN " & _
"_EMS_Reg_Impacts ON _SAFETY_Risks.HAZARD = _EMS_Reg_Impacts.ID " & _
"WHERE (_SAFETY_Risks.REC_COUNT = 0) AND (_EMS_Reg_Impacts.REC_COUNT = 0) AND ASPECTID = '" & lstItems.SelectedItem.Value & "'"
Dim ds2 As New DataSet
ds2 = currCC.executeQuery(Application("ConnString"), strSQL1, "PPE")
'Create Data Row
Dim r2 As DataRow
For Each r2 In ds2.Tables("PPE").Rows
Dim cell1 As New System.Web.UI.WebControls.TableCell
Dim cell2 As New System.Web.UI.WebControls.TableCell
Dim cellremove As New System.Web.UI.WebControls.TableCell
Dim tr As New System.Web.UI.WebControls.TableRow
cell1.Text = currCC.retrieveString("RISK", r2)
cell2.Text = currCC.retrieveString("HAZARDDESC", r2)
cell1.CssClass = "content3"
cell2.CssClass = "content3"
cellremove.CssClass = "content3"
tr.Cells.Add(cell1)
tr.Cells.Add(cell2)
Dim cmdRemove As New System.Web.UI.HtmlControls.HtmlButton
cmdRemove.Attributes.Add("onclick", "removerisks('" & r2("ID") & "')")
cmdRemove.InnerText = "Remove"
cmdRemove.Style.Add("font-size", "10px")
cellremove.Controls.Add(cmdRemove)
tr.Cells.Add(cellremove)
tblRisks.Rows.Add(tr)
makeImpactRow(ImpArray(counter), RiskCounter, r2)
RiskCounter += 1
Next
End If
Next
I tried redoing this whole loop but separating it leads to more errors. The problem with this loop is when the page refreshes after changing an item in a list box the risks function runs through this loop twice and so duplicates the risks in so you end up with 2 copies of the same risk. I have fixed many errors on this page but this error has me stumped as to how it should be fixed.
Does anyone have any ideas how to make this code work correctly? It works fine until I change a list box and then the function just duplicates the one field.
If you only want the loop to be iterated once only, then it's simple - don't have a loop! Unless you mean that your entire function is getting called twice when it should be called once - either way, you have a logic error, and you need to fix it.
I'm not going to correct your code, because a) I don't speak VB, b) I believe that VB was made by Satan himself (refer to point a), and c) you'll only become a better coder if you learn how to debug your code yourself. Suffice to say, I assume that you're using Visual Studio - it has some useful debuggering features. Try putting some breakpoints in at key parts of the code, step through your code, keep track of what's being contained in your variables. You want to get to the point where you can say "at line X, I was expecting to see Y, but Z happened instead" - then the rest is easy.
Ron2K wrote:If you only want the loop to be iterated once only, then it's simple - don't have a loop! Unless you mean that your entire function is getting called twice when it should be called once - either way, you have a logic error, and you need to fix it.
I'm not going to correct your code, because a) I don't speak VB, b) I believe that VB was made by Satan himself (refer to point a), and c) you'll only become a better coder if you learn how to debug your code yourself. Suffice to say, I assume that you're using Visual Studio - it has some useful debuggering features. Try putting some breakpoints in at key parts of the code, step through your code, keep track of what's being contained in your variables. You want to get to the point where you can say "at line X, I was expecting to see Y, but Z happened instead" - then the rest is easy.
Ron2K wrote:If you only want the loop to be iterated once only, then it's simple - don't have a loop! Unless you mean that your entire function is getting called twice when it should be called once - either way, you have a logic error, and you need to fix it.
I'm not going to correct your code, because a) I don't speak VB, b) I believe that VB was made by Satan himself (refer to point a), and c) you'll only become a better coder if you learn how to debug your code yourself. Suffice to say, I assume that you're using Visual Studio - it has some useful debuggering features. Try putting some breakpoints in at key parts of the code, step through your code, keep track of what's being contained in your variables. You want to get to the point where you can say "at line X, I was expecting to see Y, but Z happened instead" - then the rest is easy.
I thought it was that but I been through all the code and can't find why it is doing that. I did a write response piece by this code and using debug break at it as well. I been trying the list box code to find out why it affects this piece only because the other two functions work correctly.
I was thinking to solve it I might have to rewrite the entire function. Whoever made this code did not check that it worked. Earlier I fixed javascript errors, sql errors and syntax errors.
But overall this whole application should be rewritten. It is too full of mistakes.
I try again to see what other functions interact with this one and take it from there.
Ron2K wrote:If you only want the loop to be iterated once only, then it's simple - don't have a loop! Unless you mean that your entire function is getting called twice when it should be called once - either way, you have a logic error, and you need to fix it.
I'm not going to correct your code, because a) I don't speak VB, b) I believe that VB was made by Satan himself (refer to point a), and c) you'll only become a better coder if you learn how to debug your code yourself. Suffice to say, I assume that you're using Visual Studio - it has some useful debuggering features. Try putting some breakpoints in at key parts of the code, step through your code, keep track of what's being contained in your variables. You want to get to the point where you can say "at line X, I was expecting to see Y, but Z happened instead" - then the rest is easy.
I'm with Ron2K on this one.
If I wrote this code I would not have used a loop either. This application has been upgraded from about 1998 and been fixed ever since. I am now trying to get it to work correctly.
RuadRauFlessa wrote:Ok to start off with I can see 4 loops there. Which one is it that executes twice and should not?
It seems to be the top one that is going through it twice. I tried moving the next to closer to the top but then the sqlSQL1 does not work.
I compared this function to the two that were working and it is totally different. If at all possible I need to get it out of this for loop at the top and still get it to work.
I checked it again and it seems to be the third for loop in this function. When you choose the first item the loop works but if you select the second one it goes through it twice but the third option does not work properly at all.
Ok
So where does ImpArray come from and how many values is in it?
Dim dsNumRisks As New DataSet is a waste of memory so do Dim dsNumRisks As DataSet
Any reason why txtNumRisks.Text is text and not an int
Also this loop only counts the amount of entries returned by the query. It is much better to adjust the query and only have it return a count than doing all of this. Much faster too.
I would also try and figure a way of doing all of that stuff in the loop just below it as it iterates for the same list (ImpArray).
CesarePlay wrote:I checked it again and it seems to be the third for loop in this function. When you choose the first item the loop works but if you select the second one it goes through it twice but the third option does not work properly at all.
For Each r2 In ds2.Tables("PPE").Rows
Dim cell1 As New System.Web.UI.WebControls.TableCell
Dim cell2 As New System.Web.UI.WebControls.TableCell
Dim cellremove As New System.Web.UI.WebControls.TableCell
Dim tr As New System.Web.UI.WebControls.TableRow
cell1.Text = currCC.retrieveString("RISK", r2)
cell2.Text = currCC.retrieveString("HAZARDDESC", r2)
cell1.CssClass = "content3"
cell2.CssClass = "content3"
cellremove.CssClass = "content3"
tr.Cells.Add(cell1)
tr.Cells.Add(cell2)
Dim cmdRemove As New System.Web.UI.HtmlControls.HtmlButton
cmdRemove.Attributes.Add("onclick", "removerisks('" & r2("ID") & "')")
cmdRemove.InnerText = "Remove"
cmdRemove.Style.Add("font-size", "10px")
cellremove.Controls.Add(cmdRemove)
tr.Cells.Add(cellremove)
tblRisks.Rows.Add(tr)
makeImpactRow(ImpArray(counter), RiskCounter, r2)
RiskCounter += 1
Next
RuadRauFlessa wrote:Ok
So where does ImpArray come from and how many values is in it?
Dim dsNumRisks As New DataSet is a waste of memory so do Dim dsNumRisks As DataSet
Any reason why txtNumRisks.Text is text and not an int
Also this loop only counts the amount of entries returned by the query. It is much better to adjust the query and only have it return a count than doing all of this. Much faster too.
I would also try and figure a way of doing all of that stuff in the loop just below it as it iterates for the same list (ImpArray).
ImpArray as far as I can gather is the name of the array used and at the moment it has 4 values in it. Ideally it works by counting the number of values in the array.
im ImpArray As Array
ImpArray = ImpactsArray("IMPACTS", r)
txtNumRisks gets converted to int in one function. This I know because I just tracked it.
I agree with your assessment. I am not actually sure who wrote the function though and been trying the last week to get the whole page working. If you think this part is bad you should see the rest of the page.
One guy where I work thinks that this function is been called twice but we can't find out where. The whole code is a mess.
For counter = 0 To (ImpArray.Length - 1)
If IsNumeric(ImpArray(counter)) Then
'==============Get Impact Name==================
strSQL = "SELECT * FROM _EMS_Reg_Impacts WHERE ID = '" & ImpArray(counter) & "'"
Dim dsImpName As New DataSet
dsImpName = currCC.executeQuery(Application("ConnString"), strSQL, "ImpName")
Dim rImpName As DataRow
If dsImpName.Tables("ImpName").Rows.Count > 0 Then
rImpName = dsImpName.Tables("ImpName").Rows(0)
'============HAZARDS DROP-DOWN=============
Dim currItem As New System.Web.UI.WebControls.ListItem
currItem.Value = ImpArray(counter)
currItem.Text = rImpName("IMPACT")
lstHazards.Items.Add(currItem)
'=========================================
End If
'GET RISKS
Dim strSQL1 = "SELECT _SAFETY_Risks.*, _EMS_Reg_Impacts.IMPACT AS HAZARDDESC " & _
"FROM _SAFETY_Risks INNER JOIN " & _
"_EMS_Reg_Impacts ON _SAFETY_Risks.HAZARD = _EMS_Reg_Impacts.ID " & _
"WHERE (_SAFETY_Risks.REC_COUNT = 0) AND (_EMS_Reg_Impacts.REC_COUNT = 0) AND ASPECTID = '" & lstItems.SelectedItem.Value & "'"
'Response.Write(strSQL1 & "<br>")
Dim ds2 As New DataSet
ds2 = currCC.executeQuery(Application("ConnString"), strSQL1, "PPE")
'Create Data Row
Dim r2 As DataRow
For Each r2 In ds2.Tables("PPE").Rows
Dim cell1 As New System.Web.UI.WebControls.TableCell
Dim cell2 As New System.Web.UI.WebControls.TableCell
Dim cellremove As New System.Web.UI.WebControls.TableCell
Dim tr As New System.Web.UI.WebControls.TableRow
cell1.Text = currCC.retrieveString("RISK", r2)
cell2.Text = currCC.retrieveString("HAZARDDESC", r2)
cell1.CssClass = "content3"
cell2.CssClass = "content3"
cellremove.CssClass = "content3"
tr.Cells.Add(cell1)
tr.Cells.Add(cell2)
Dim cmdRemove As New System.Web.UI.HtmlControls.HtmlButton
cmdRemove.Attributes.Add("onclick", "removerisks('" & r2("ID") & "')")
cmdRemove.InnerText = "Remove"
cmdRemove.Style.Add("font-size", "10px")
cellremove.Controls.Add(cmdRemove)
tr.Cells.Add(cellremove)
tblRisks.Rows.Add(tr)
'makeImpactRow(RiskCounter, r2)
makeImpactRow(ImpArray(counter), RiskCounter, r2)
RiskCounter += 1
Next
End If
Next
RuadRauFlessa wrote:So it actually runs trough that code 8 times. (4 values in array x 2).
That said you are propably getting double entries in the list box on the page too
No I was getting double values in the table it populates. When I use the response.write in that piece it gets written twice and the table gets duplicate rows with the same data. Earlier today it duplicated the same code 8 times with the results I had 8 x water hazard. Now however it only duplicates the data in the table.
The table the data goes in is tblRisks It is a dynamic table that only appears once data is added to it.
Yes. Thats exactly what is happening but when you go into sql the data is not duplicated there. It is only duplicating on the table in the application.