protected void MetaCheck_Click(object sender, EventArgs e)
    {
        if ((CheckBoxList1.Items.Cast <ListItem>().Count(li => li.Selected)) == 0)
        {
            Label2.Text          = "Please select WAP servers to view Metadata response.";
            Label2.ForeColor     = System.Drawing.ColorTranslator.FromHtml("#FF0000");
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
        else
        {
            Label2.Text = "";
            AdfsSqlHelper stsName        = new AdfsSqlHelper();
            string        sts            = stsName.GetFarmName();
            var           SelectedWAPs   = CheckBoxList1.Items.Cast <ListItem>().Where(i => i.Selected).Select(i => i.ToString()).ToArray();
            DataTable     MetaCheckTable = new DataTable();
            MetaCheckTable.Columns.Add("WAP Server Name");
            MetaCheckTable.Columns.Add("IP");
            MetaCheckTable.Columns.Add("Response URL");
            MetaCheckTable.Columns.Add("Response");

            foreach (object WAP in SelectedWAPs)
            {
                IPAddress[] ipaddress = Dns.GetHostAddresses(WAP.ToString());

                DataRow row = MetaCheckTable.NewRow();
                row["WAP Server Name"] = WAP.ToString();
                foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
                {
                    row["IP"] = (ip4.ToString());
                    //
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://" + sts + "/federationmetadata/2007-06/federationmetadata.xml");//url and Host header
                    FieldInfo      field_ServicePoint_ProxyServicePoint = (typeof(ServicePoint))
                                                                          .GetField("m_ProxyServicePoint", BindingFlags.NonPublic | BindingFlags.Instance);
                    req.Proxy = new WebProxy(ip4.ToString() + ":443");//server IP and port
                    field_ServicePoint_ProxyServicePoint.SetValue(req.ServicePoint, false);
                    //req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                    try
                    {
                        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                        //Label9.Text = resp.Cookies.ToString();
                        row["Response URL"] = resp.ResponseUri.ToString();
                        row["Response"]     = resp.StatusCode.ToString();
                    }
                    catch (Exception MetadataExp)
                    {
                        //Label9.Text = MetadataExp.ToString();
                        row["Response URL"] = MetadataExp.Message.ToString();
                        row["Response"]     = MetadataExp.Message.ToString() + " Check Farname is correct under configuration tab and servers have correct SNI bindings.";
                        //Label1.Text = "Check Farname is correct under configuration tab and servers have correct SNI bindings.";
                        //Label1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
                    }
                }
                MetaCheckTable.Rows.Add(row);
            }

            GridView1.DataSource = MetaCheckTable;
            GridView1.DataBind();
        }
    }
    }     //botton end

    protected void GetDbConnectivity_Click(object sender, EventArgs e)
    {
        Label1.Text          = "";
        Label2.Text          = "";
        GridView3.DataSource = null;
        GridView3.DataBind();
        if ((CheckBoxList1.Items.Cast <ListItem>().Count(li => li.Selected)) == 0)
        {
            Label2.Text      = "Please select WAP servers to view DB connectivity.";
            Label2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
        }
        else
        {
            var       SelectedWAPs = CheckBoxList1.Items.Cast <ListItem>().Where(i => i.Selected).Select(i => i.ToString()).ToArray();
            DataTable WAPConTable  = new DataTable();
            WAPConTable.Columns.Add("WAP Server Name");
            WAPConTable.Columns.Add("DBConnectivity");
            WAPConTable.Columns.Add("HealthState");
            WAPConTable.Columns.Add("CommandRunStatus");

            foreach (object WAP in SelectedWAPs)
            {
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {
                    PowerShellInstance.AddCommand("invoke-command");
                    PowerShellInstance.AddParameter("ComputerName", WAP);
                    ScriptBlock filterAllWAPDBCon = ScriptBlock.Create("Get-WebApplicationProxyConfiguration | select ConfigurationVersion, ConfigurationChangesPollingIntervalSec -ErrorAction Stop");
                    PowerShellInstance.AddParameter("ScriptBlock", filterAllWAPDBCon);

                    foreach (PSObject outputItemDCCon in PowerShellInstance.Invoke())
                    {
                        DataRow row = WAPConTable.NewRow();
                        row["WAP Server Name"]  = (outputItemDCCon.Members["PSComputerName"].Value).ToString();
                        row["DBConnectivity"]   = (outputItemDCCon.Members["ConfigurationVersion"].Value).ToString();
                        row["HealthState"]      = (outputItemDCCon.Members["ConfigurationChangesPollingIntervalSec"].Value).ToString() + " Sec Poll";
                        row["CommandRunStatus"] = "Success";
                        WAPConTable.Rows.Add(row);
                    }

                    // if error was here
                    if (PowerShellInstance.Streams.Error.Count > 0)
                    {
                        DataRow row    = WAPConTable.NewRow();
                        var     errors = PowerShellInstance.Streams.Error;
                        var     PS     = PowerShellInstance;
                        var     sb     = new StringBuilder();
                        foreach (var error in errors)
                        {
                            sb.Append(error); // changed here
                        }
                        string errorResult = sb.ToString();
                        row["WAP Server Name"]  = WAP.ToString(); //you were here trying to find the computer that generated the error while backend server was inaccessible
                        row["DBConnectivity"]   = errorResult;
                        row["HealthState"]      = "Error";
                        row["CommandRunStatus"] = "Error";
                        WAPConTable.Rows.Add(row);
                    }
                } //PowerShell instance
            }
            GridView3.DataSource = WAPConTable;
            GridView3.DataBind();
        } //Else end
    }     //button end
    protected void GetHealth_Click(object sender, EventArgs e)
    {
        Label1.Text          = "";
        Label2.Text          = "";
        GridView2.DataSource = null;
        GridView2.DataBind();
        if ((CheckBoxList1.Items.Cast <ListItem>().Count(li => li.Selected)) == 0)
        {
            Label2.Text      = "Please select WAP servers to view health.";
            Label2.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
        }
        else
        {
            var       SelectedWAPs = CheckBoxList1.Items.Cast <ListItem>().Where(i => i.Selected).Select(i => i.ToString()).ToArray();
            DataTable WAPtempTable = new DataTable();
            WAPtempTable.Columns.Add("WAP Server Name");
            WAPtempTable.Columns.Add("Component");
            WAPtempTable.Columns.Add("HealthState");
            WAPtempTable.Columns.Add("Heuristics");
            WAPtempTable.Columns.Add("CommandRunStatus");

            foreach (object WAP in SelectedWAPs)
            {
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {
                    PowerShellInstance.AddCommand("invoke-command");
                    PowerShellInstance.AddParameter("ComputerName", WAP);
                    ScriptBlock filterAllWAPsHealth = ScriptBlock.Create("Get-WebApplicationProxyHealth | select RemoteAccessServer, Component, HealthState, Heuristics");
                    PowerShellInstance.AddParameter("ScriptBlock", filterAllWAPsHealth);
                    foreach (PSObject outputItem in PowerShellInstance.Invoke())
                    {
                        DataRow row = WAPtempTable.NewRow();

                        row["WAP Server Name"] = (outputItem.Members["PSComputerName"].Value).ToString();
                        row["Component"]       = (outputItem.Members["Component"].Value).ToString();
                        row["HealthState"]     = (outputItem.Members["HealthState"].Value).ToString();
                        if ((outputItem.Members["Heuristics"].Value).ToString() != null)
                        {
                            row["Heuristics"] = (outputItem.Members["Heuristics"].Value).ToString();
                        }
                        row["CommandRunStatus"] = "Success";

                        WAPtempTable.Rows.Add(row);
                    }

                    if (PowerShellInstance.Streams.Error.Count > 0)
                    {
                        DataRow row    = WAPtempTable.NewRow();
                        var     errors = PowerShellInstance.Streams.Error;
                        var     sb     = new StringBuilder();
                        foreach (var error in errors)
                        {
                            sb.Append(error);
                        }
                        string errorResult = sb.ToString();
                        row["WAP Server Name"]  = WAP.ToString();
                        row["Component"]        = errorResult;
                        row["HealthState"]      = errorResult;
                        row["HealthState"]      = errorResult;
                        row["CommandRunStatus"] = errorResult;
                        WAPtempTable.Rows.Add(row);
                    }
                } //Powershell Instance 1
            }     //For each end
            GridView2.DataSource = WAPtempTable;
            GridView2.DataBind();
        } //Else End
    }     //botton end