Note that there are some explanatory texts on larger screens.

plurals
  1. POpopulate array list from For loop using entire database table row
    text
    copied!<p>I am trying check my <code>Events</code> table for DateTime overlaps in each of the events. I have written code that will allow a user to input a <code>Module_code</code>. This <code>Module_code</code> is checked against the entries in the <code>Events</code> table. If <code>Start_Date_Time</code> or <code>End_Date_Time</code> for any of the events overlap with the <code>Start_Date_Time</code> or <code>End_Date_Time</code> of any other event in the table, for that module only, then I want to put the event row into an array list and later populate a gridview with the results. I have the following code, which I think achieves most of my goals. However when I run it the gridview there is no data in it. Debug shows that nothing was sent to it. I presume it is a problem with how I am trying to pass my values to the arrayList. </p> <pre><code> 'return all relevant tables from the Modules database, based on the module code entered by the user. Dim eventTime = (From mods In db.Modules Join evnt In db.Events On mods.Module_code Equals evnt.Module_code Join rm In db.Rooms On rm.Room_ID Equals evnt.Room_ID Join build In db.Buildings On build.Building_code Equals rm.Building_code Where ((mods.Module_code = initialModCode) And (evnt.Room_ID = rm.Room_ID)) Select evnt.Event_ID, evnt.Module_code, evnt.Event_type, evnt.Start_Date_Time, evnt.End_Date_Time, build.Building_code, rm.Room_Number) Dim listClashes As New ArrayList() For i As Integer = 0 To eventTime.Count - 1 For j As Integer = i + 1 To eventTime.Count - 1 If (eventTime.ToList(i).Start_Date_Time &lt; eventTime.ToList(j).End_Date_Time) And (eventTime.ToList(i).End_Date_Time &gt; eventTime.ToList(j).Start_Date_Time) Then 'MsgBox("Clash", MsgBoxStyle.MsgBoxSetForeground, "") listClashes.Add(eventTime) Else 'MsgBox("No Clash", MsgBoxStyle.MsgBoxSetForeground, "") End If Next Next gdvClashes.DataSource = listClashes gdvClashes.DataBind() </code></pre> <p>EDIT..Code changed to</p> <pre><code> If (eventTime(i).Start_Date_Time &lt;= eventTime(j).End_Date_Time) And (eventTime(i).End_Date_Time &gt;= eventTime(j).Start_Date_Time) Then 'MsgBox("Clash", MsgBoxStyle.MsgBoxSetForeground, "") listClashes.Add(eventTime.ToList) Else </code></pre> <p>This edit has not worked. It is throwing an exception to say that <code>The query operator 'ElementAtOrDefault' is not supported.</code> Am I going in the totally wrong direction here?</p> <p>EDIT..</p> <p>New code suggestion</p> <pre><code> Dim listClashes = From e1 In eventTime From e2 In eventTime Where (e2.Start_Date_Time &gt;= e1.Start_Date_Time) And (e2.Start_Date_Time &lt;= e1.End_Date_Time) Select eventTime gdvClashes.DataSource = listClashes gdvClashes.DataBind() </code></pre> <p>Should this be enclosed in a for loop or referencing an arrayList or something?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload