Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please how do i refresh a my webpage at a button click?

i want to my form to show in my GridView immediately after submitting the records in my data base.

i want the page to refresh itself automatically.

C#
protected void BtnAddTraining_Click(object sender, EventArgs e)
    {
   bool exist = false;
    string sqcommand = "SELECT count(*) FROM fl_poltype WHERE poltype=@poltype";
    using (SqlCommand cmd = new SqlCommand(sqcommand, cn))
    {
        cn.Open();
        cmd.Parameters.AddWithValue("poltype", TxtPolicyCode.Text);
        exist = (int)cmd.ExecuteScalar() > 0;
    }
    //clear the filed if exist else, insert record
    if (exist)
    {
        LblError.Text = "user already exist";
        TxtPolicyCode.Focus();
        TxtPolicyCode.Text = string.Empty;
        return;

    }
    else
    {
        try
        {

            string insertCommand = "INSERT Into fl_poltype(poltype,poldesc,maxloan,income_account,liability_account,expense_account,vat_account) values(@poltype,@poldesc,@maxloan,@income_account,@liability_account,@expense_account,@vat_account)";
            SqlCommand cmd = new SqlCommand(insertCommand, cn);

            cmd.Parameters.AddWithValue("@poltype", TxtPolicyCode.Text);
            cmd.Parameters.AddWithValue("@poldesc", TxtPolicyName.Text);
            cmd.Parameters.AddWithValue("@maxloan", TxtMaxLoan.Text);
            cmd.Parameters.AddWithValue("@income_account", ddlIncomeCode.SelectedValue);
            cmd.Parameters.AddWithValue("@liability_account", ddlIncomeCode.SelectedValue);
            cmd.Parameters.AddWithValue("@expense_account", ddlLiabilityCode.SelectedValue);
            cmd.Parameters.AddWithValue("@vat_account", ddlVatCode.SelectedValue);



            cmd.ExecuteNonQuery();
            LblError.Text = "SUCCESSFULLY ADDED";
            cleared();
            
            cn.Close();
        }
        catch (Exception ex)
        {
            string log = ex.Message;
        }
        finally
        {
            cn.Close();
        }
    }     

    }
Posted
Updated 7-Sep-16 22:52pm
v2

1 solution

Either re-bind your gridview after updating the database (you haven't shown how you bind it in the first place so we can't tell you how to do that), or an alternative is to do

Response.Redirect("yourpagename.aspx");

after inserting the data. That will causes the page to reload fresh and will avoid duplicate posting issues if the user refreshes the page after, or goes "back" to the page from a subsequent one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900