title: DataList ASP.net c# looping through
body: <p>I have a Datalist that pulls data from an SQL database that contains Servers Names. (6 of )</p>
  
  <p>I would like to loop through the Datalist and for each server in the list connect to it using WMI and see if a value is true or not.</p>
  
  <p>I have this working for one server name but not when i combine it with the datalist.</p>
  
  <p>Where am i going wrong?</p>
  
  <p>Thanks</p>
  
  <pre><code> string SARHServerName = "";
   //SARHServerName = reader["ServerName"].ToString();
  
   ConnectionOptions con = new ConnectionOptions();
   con.Username = "Username";
   con.Password = "Password";
  
   foreach (DataListItem item in DataList.Items)
   {
  
        Label1.Text = SARHServerName;
        SARHServerName += "," + ((Label)    (item.FindControl("ServerNameLabel"))).Text;
        ManagementScope scope =
          new ManagementScope(
          "\\\\" + SARHServerName + "\\root\\CIMV2\\TerminalServices", con);
        scope.Options.EnablePrivileges = true;
        scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
        scope.Options.Impersonation = ImpersonationLevel.Impersonate;
        scope.Connect();
  
        //create object query
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalServiceSetting");
  
        //create object searcher
        ManagementObjectSearcher searcher =
                                              new ManagementObjectSearcher(scope,     query);
  
        //get collection of WMI objects
        ManagementObjectCollection queryCollection = searcher.Get();
  
        //enumerate the collection.
        foreach (ManagementObject m in queryCollection)
        {
            if (m["Logons"].ToString() == "0")
            {
                ((Image)(item.FindControl("ServerDown"))).Visible = true;
                ((Image)(item.FindControl("ServerUP"))).Visible = false;
            }
            else
            {
                ((Image)(item.FindControl("ServerDown"))).Visible = false;
                ((Image)(item.FindControl("ServerUP"))).Visible = false;
            }
         }
    }
  </code></pre>
  
  <p>I am calling this function once i have bound the datasource to the Datalist.</p>
  
  <pre><code>public void GetDataFromSQLGridView()
  {
  
  SqlConnection conn = null;
  
      try
       {
  
  conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DailyChecksConnectionString"].ConnectionString);
  
              {
                  conn.Open();
                  using (SqlCommand cmd =
                      new SqlCommand("SELECT ServerName FROM Servers_Checks", conn))
                  {
  
                      DataListSQL.DataSource = cmd.ExecuteReader();
                      DataListSQL.DataBind();
                  }
  
              }
  
          }
          catch { }
          finally { GetRemoteDesktop(); }
  
  
  }
  </code></pre>
  
  <p>Would it be my SQL command? It looks like its returning all the server names from SQL in one go instead of returning one then moving on and pull another record.</p>
  
  <p>Ok i have found some further information on this. I did a </p>
  
  <pre><code>Response.Write(SARHServerName);
  Response.Write("&lt;br /&gt;");
  </code></pre>
  
  <p>And its going through the loop fine but its concatenating the results like this</p>
  
  <pre><code>server1
  server1server2
  server1server2server3
  server1server2server3server4
  server1server2server3server4server5
  server1server2server3server4server5server6
  </code></pre>
  
  <p>I must be close?</p>