Hello everyone ,
Have a small problem which i dont know how to deal with . at the same time not sure if i need delegate or event handlers . here it goes .
I have two classes with two forms : Form A and Progress Form .
Form A contains a button where when the user clicks on it , a sql connection is connected and a query is executed ( which contains lets say a million data and take quite some time to execute it ) .
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=.//SQLEXPRESS ;Initial Catalog=GRCEXPLORER;Integrated Security = true");
try
{
// connect to the server
string CrLF = Environment.NewLine;
ProgressForm progressForm = new ProgressForm(conn);
progressForm.Show();
progressForm.Refresh();
conn.Open();
progressForm.Visible = true;
progressForm.abbrechen_Click( sender, e);
/*SqlCommand cmd = new SqlCommand("UPLOAD" + comboBox2.Text, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UPLOAD" + comboBox2.Text;
cmd.Parameters.Add(new SqlParameter ("@auditconnector", SqlDbType.NVarChar, 50));
cmd.Parameters["@auditconnector"].Value = comboBox10.Text;
comboBox10.Items.AddRange(new[] { "20100808" }); comboBox10.SelectedItem = ("20100802");
cmd.CommandTimeout = 0;
SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) Console.WriteLine(dr[""]); dr.Close(); conn.Close(); Console.ReadLine();
*/
progressForm.Close();
conn.Close();
}
catch(Exception Ex)
{
if (conn != null)
conn.Dispose();
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Ex.Message;
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
For this i have created another class ProgressFor which contains a Cancel button. i.e if the user clicks on the Cancel button the connection to the sql server is closed
public void abbrechen_Click(object sender, EventArgs e)
{
// testdelegate del = new testdelegate(this.abbrechen_Click) ;
if (sqlConn.State != 0 )
sqlConn.Close();
}
My Problem : Progress Form opens but the control is sent back to Form A( where the query is executing ) becaue of which cannot click on the Form B Cancel button.
would appreciate any help