Last post Feb 13, 2015 04:56 AM by Zhi Lv - MSFT
Feb 12, 2015 10:22 AM|bryanbankester|LINK
Mission: Compare array (populated from dataset) every 5 seconds to the previous array.
Method: I'm using a ASP Timer.
Problem: array1 (SUPPOSED TO BE populated 10 seconds earlier than array2 BUT IS REPOPULATED EVERY TIME ARRAY2 IS) and array2 (populated 10 seconds after array1) both change/repopulate every 10 seconds. So, array1 and array2 are always identical.
Question: How can I keep array1 from changing? OR- How would you achieve the same "Mission" that I am?
<asp:ScriptManager runat="server" id="ScriptManager1" />
<asp:UpdatePanel runat="server" id="UpdateCams" UpdateMode="Conditional" ChildrenAsTriggers="false">
<asp:Timer runat="server" id="Timer1" Interval="5000" OnTick="Timer1_Tick" ></asp:Timer>
<asp:Label id="Label1" runat="server"></asp:Label>
Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs)
Dim dsArray As DataSet
dsArray = fillDataset("SELECT machine_id FROM [machines] where machine_type ='" & Request.QueryString("machtype") & "'", connstring, False)
Dim array2(dsArray.Tables(0).Rows.Count) As String
For x As Integer = 0 To dsAfterArray.Tables(0).Rows.Count - 1
array2(x) = dsAfterArray.Tables(0).Rows(x).Item("active_override")
For y=0 to dsArray.Tables(0).Rows.Count - 1
If array2(y) <> array1(y) Then 'Problem is that array2 is always equal to array1
UpdateCams.Update() 'Update only when array1 element is not equal to an array2 element
I am populating array1 like this and have tried calling LoadArray1() all over the ASP page. No matter where I call it, it re-calls LoadArray1() with every 5 second time interval.
Dim j As Integer = 0
Dim dsBeforeArray As DataSet
dsBeforeArray = fillDataset("SELECT machine_id FROM machines WHERE mach_type='" & Request.QueryString("machtype") & "'", connstring, False)
Dim row As DataRow
For Each row In dsBeforeArray.Tables(0).Rows
array1(j) = row.Item("machine_id")
j = j + 1
Feb 12, 2015 10:41 AM|AidyF|LINK
You have to remember that your objects only exist while the page is rendering, when your page is finished and sent to the client everything is destroyed. So your array1 is being built each time. If you want array1 to survive then store it in the session.
c# but something like
private void LoadArray1()
if (Session["a1"] != null)
array1 = (arrayType)Session["a1"];
array1 = GetArrayFromDatabase();
Session["a1"] = array1;
Feb 12, 2015 11:00 AM|bryanbankester|LINK
I thought the UpdatePanel.Update() every timer interval 5 seconds would cause only Label1 to "update" and also do whatever is inside Sub Timer1_Tick. But apparently it is causing my array1, the previous array to being built every 5 seconds. That does not
make sense to me how Timer1 and UpdatePanel1 could interfere with Sub LoadArray1(). Since Im not calling LoadArray1() inside the UpdatePanel or inside Sub Timer1_Tick.
Feb 12, 2015 11:05 AM|AidyF|LINK
You haven't shown where you are calling LoadArray1 so it's hard to give an answer.
Forget the updatepanel though, it makes no difference to the page lifecycle, everything is getting created then destroyed each 5 seconds. With the update panel it is no different to a normal page request, the only difference is that a limited set of the
html is returned - it doesn't just call your click event, it runs everything, the whole page_load etc. Imagine every timer click is you opening the page in a new browser, that's what is happening, your page is not storing any state.
Feb 13, 2015 04:56 AM|Zhi Lv - MSFT|LINK
As for this issue, I agree with AidyF, you could use session to store the array1 and array 2.
Besides, I also have some problems about this issue.
After compare the two arrays, do you populate the array2? And, after 10 seconds, which event will be triggered first, compare event or repopulate event?
So, I suppose perhaps this issue it also related to the time. I suggest you could set the compare time greater than the Array repopulate time.