I need to display two fields from my SQL database in dynamically created labels. So for every entry into the database it needs to display it on the main window in labels underneath each other. But it is not showing the info in the label.
So this is what needs to happen.
the users saves the info into the SQL database (Host name, IP address and Device)
on the main screen (windows) it then needs to be displayed like this:
Server 1 192.168.x.x "Ping Result"
This is my code I have at them moment (this does not include the ping results label yet)
private void AddDynamicLabels()
{
string ConString = @"Data Source=PC\SQL;Initial Catalog=PingMonitorDB;Integrated Security=True";
SqlConnection con = new SqlConnection(ConString);
string CmdString = "SELECT host, ip FROM addhosts";
SqlCommand cmd = new SqlCommand(CmdString, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label lbl = new Label();
lbl.Visible = true;
lbl.Text = reader["host" + "ip" + " "].ToString();
lbl.BackColor = System.Drawing.Color.Orange;
this.Controls.Add(lbl);
}
con.Close();
}
So for every entry added it should in theory look like this:
Server 1 192.168.x.x "ping reply"
Server 2 192.168.x.x "ping reply"
Server 3 192.168.x.x "ping reply"
....
etc
EDIT
private async void frmMainWindow_Load(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(@"Data Source=PC\SQL;Initial Catalog=PingMonitorDB;Integrated Security=True");
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
SqlDataAdapter dp = new SqlDataAdapter();
DataTable dtTable = new DataTable();
cmd.CommandText = "SELECT host, ip FROM addhosts";
cmd.CommandType = CommandType.Text;
dp.SelectCommand = cmd;
dp.Fill(dtTable);
cnn.Close();
for (int i = 0; i < dtTable.Rows.Count; i++)
{
ipAddress.Add("ip");
Label lbl = new Label();
lbl.Visible = true;
lbl.AutoSize = true;
lbl.Font = new Font("Arial", 12, FontStyle.Bold);
lbl.Text = dtTable.Rows[i]["host"].ToString() + " " + dtTable.Rows[i]["ip"].ToString();
lbl.BackColor = System.Drawing.Color.Orange;
this.Controls.Add(lbl);
flowLayoutPanel1.Controls.Add(lbl);
var controls = new Control[] { /* a list of existing Controls */ };
// The Addresses count must match the Controls'
var addresses = ["0.0.0.0"]; => Not sure what to do here though?
var massPing = new MassPing();
await massPing.PingAll(addresses, controls, 2000);
}
}
The the MassPing
Class is shown in a previous question:
How to change the image of a PictureBox when the Ping Round Trip Time is more that 50ms?