title: ASP.NET Overloaded method on Foreach loop within a List
body: <p>In the below code I am trying to have a session variable that contains a List of strings which holds all of the selected ModuleId's. I am using the module_Changed event to add or remove moduleIDs from this List. Then calling the getModulesAtCurrentSlot in a loop for each moduleid in the list and concatenate the returned string[]s into one longer string[] or List which to display.</p>
  
  <p>I am getting the error "The name 'timeslotInt' and 'Day' does not exist in the current context". The actual getModulesAtCurrentSlot method is located in a separate .cs file so Ive used dba.getModulesAtCurrentSlot... to reference the method but I'm still getting those errors. </p>
  
  <p>.aspx</p>
  
  <pre><code>&lt;asp:TemplateField&gt;
     &lt;ItemStyle HorizontalAlign="Center" Width="40px"&gt;&lt;/ItemStyle&gt;
             &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID="chkBox2" runat="server" ToolTip='&lt;%# Eval("ModuleId") %&gt;' OnCheckedChanged="module_Changed" /&gt;
             &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>.cs </p>
  
  <pre><code>  protected void module_Changed(object sender, EventArgs e)
      {
          List&lt;string&gt; lst;
          if (Session["lst"] != null)
              lst = (List&lt;string&gt;)Session["lst"];
          else
              Session.Add("lst", new List&lt;string&gt;());
  
          // Retrieve the check box ModuleId value to add to my SELECT query
          string moduleid = ((CheckBox)sender).ToolTip;
  
          // add your own code to check if checkbox is checked or unchecked to see if you need to add or remove the ID from the list
  
          // to add
          if (lst.Contains(moduleid) == false)
              lst.Add(moduleid);
  
          // to remove - add your own code
  
          int timeslotInt = 0;
          String Day = "text";
  
          List&lt;string&gt; lstResult = new List&lt;string&gt;();
          foreach (var moduleID in lst)
          { 
            lstResult.Add(dba.getModulesAtCurrentSlot(timeslotInt, module, Day);
          }
  
          // do something to display lstResult
          Gridview1.DataSource = lstResult;
          Gridview1.DataBind();
      }
  
  }
  </code></pre>
  
  <p>DBAccess.cs</p>
  
  <pre><code>   public String[] getModulesAtCurrentSlot(int timeslotInt, String moduleID, String Day)
      {
          List&lt;String&gt; modulesList = new List&lt;string&gt;();
          if (conn.State.ToString() == "Closed")
          {
              conn.Open();
          }
          SqlCommand newCmd = conn.CreateCommand();
          newCmd.Connection = conn;
          newCmd.CommandType = CommandType.Text;
          newCmd.CommandText = "SELECT DISTINCT Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color" +
                   " FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
                                  " Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND " + timeslotInt.ToString() + " BETWEEN ClassScheduleStartTimeId and ClassScheduleEndTimeId";
          SqlDataReader dr = newCmd.ExecuteReader();
          while (dr.Read())
          {
              //Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color
              //String current = "&lt;div class='slot'&gt;";
              String current = "&lt;div class='slot' " + (!dr.IsDBNull(6) ? "style=\"background-color: " + dr.GetString(6) + ";\"" : "") + "&gt;";
              current += "&lt;div class='line1'&gt;" + dr.GetString(0) + "&amp;nbsp;" + dr.GetString(1) + "&lt;/div&gt;";// +"&lt;br /&gt;";
              current += "&lt;div class='line2'&gt;" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "&amp;nbsp;-&amp;nbsp;" + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "&lt;/div&gt;";// +"&lt;br /&gt;";
              current += "&lt;div class='line3'&gt;" + dr.GetString(4) + "&amp;nbsp;" + dr.GetString(5) + "&lt;/div&gt;";
              current += "&lt;/div&gt;";
              modulesList.Add(current);
          }
          conn.Close();
          return modulesList.ToArray();
      }
  </code></pre>
  
  <p>Error</p>
  
  <p><a href="https://i.stack.imgur.com/2iO0h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2iO0h.png" alt="enter image description here"></a></p>