Question

I have a Datalist that pulls data from an SQL database that contains Servers Names. (6 of )

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.

I have this working for one server name but not when i combine it with the datalist.

Where am i going wrong?

Thanks

 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;
          }
       }
  }

I am calling this function once i have bound the datasource to the Datalist.

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(); }


}

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.

Ok i have found some further information on this. I did a

Response.Write(SARHServerName);
Response.Write("<br />");

And its going through the loop fine but its concatenating the results like this

server1
server1server2
server1server2server3
server1server2server3server4
server1server2server3server4server5
server1server2server3server4server5server6

I must be close?

Was it helpful?

Solution

Change:

SARHServerName = ((Label)    (item.FindControl("ServerNameLabel"))).Text;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top