This is my background work DoWor function, is the implementation taking into consideration the GUI operation done okay?
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string query = "SELECT * FROM dbo.Liguanea_Lane2";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string scode = dr.GetString(dr.GetOrdinal("code"));
comboBox2.Invoke((MethodInvoker)delegate
{
comboBox2.Items.Add(scode);
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
And this is the function that calls it:
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox4.SelectedIndex == 0)
{
backgroundWorker1.RunWorkerAsync();
}
else
{
MessageBox.Show("This table doesn't exist within the database");
}
}
Currently nothing happens..the code just runs with the default form on the screen. Not loading the values, what am I doing wrong?