简体   繁体   中英

I need to reload the form on button click

I have been trying different methods these past days to overcome this problem but I haven't found a reliable solution to my problem. The problem is I have to reload the form in case the first time the application starts and the components are not filled up due to network or SQL connectivity issue. As of now, everyone is closing the application and starting again because the first time they start the application the components were empty.

Below is the code I'm using.

private void Reports_Load(object sender, EventArgs e)
{
    //START:
    try
    {
        RevenueDate_dt = func.getResult(dateQuery);
        foreach (DataRow dr in RevenueDate_dt.Rows)
        {
            comboBox1.Items.Add(dr["Global_Period_Month"].ToString());
        }
    }
    catch (Exception ex)
    {
        //goto START;
        MessageBox.Show("Network is too slow! Please close the application and try again later");
    }
}

Function:

public DataTable getResult(string query)
{
    //con.Open();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.CommandTimeout = 0;
    da.SelectCommand = cmd;
    da.Fill(dt);
    return dt;
}

I have placed a retry button on my form so if the first time the component is not filled with the data so everyone can use the retry button to load the component instead of closing the whole application and starting again.

You can fill component by calling Reports_Load event in your retry button . Reports_Load will fill your component. Check below code.

CODE:

private void retry_Click(object sender, EventArgs e)
{
    Reports_Load(null, null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM