Search code examples
asp.netpostback

Prevent data being saved when refreshing page


The issue is: I click Button1 to save data to my sqldatabase. After this my ClearAllData runs and all my fields empty. the problem is when I refresh the page, new data is entered into my sqldatabase even though all my fields are blank and I do not click Button1.

I have !IsPostback on page load but I am guessing there is something else I need to prevent the issue.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    public void ClearAllData()
    {
        DropDownList1.SelectedValue = DropDownList1.Items[0].ToString();
        DropDownList2.SelectedValue = DropDownList2.Items[0].ToString();
        DropDownList3.SelectedValue = DropDownList3.Items[0].ToString();
        DropDownList4.SelectedValue = DropDownList4.Items[0].ToString();
        DropDownList5.SelectedValue = DropDownList5.Items[0].ToString();
        TextBox1.Text = "";
        Label1.Text = "";

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(DropDownList5.SelectedValue))
        {
            SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
            con.Open();
            SqlCommand comm = new SqlCommand("Update Dispatcher_Roles set Name = '" + DropDownList1.SelectedValue + "',Position = '" + DropDownList2.SelectedValue + "',Roles = '" + TextBox1.Text + "',Status = '" + DropDownList3.SelectedValue + "',DispatcherCovering = '" + DropDownList4.SelectedValue + "' where Name='" + DropDownList1.SelectedValue + "'", con);
            comm.ExecuteNonQuery();
            con.Close();
            Label1.Text = "Update Saved";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
            ClearAllData();
        }
        if (!string.IsNullOrEmpty(DropDownList1.SelectedValue))
        {
            SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
            con.Open();
            SqlCommand comm = new SqlCommand("Insert into Dispatcher_Roles values ('" + DropDownList1.SelectedValue + "','" + DropDownList2.SelectedValue + "','" + TextBox1.Text + "','" + DropDownList3.SelectedValue + "','" + DropDownList4.SelectedValue + "')", con);
            comm.ExecuteNonQuery();
            con.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Added');", true);
            Label1.Text = "Entry Saved";
            ClearAllData();
        }
        if (DropDownList1.SelectedValue == "" || DropDownList2.SelectedValue == "" || TextBox1.Text == "")
        {
            Label1.Text = "Fill In All Fields";
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("Update Dispatcher_Roles set Name = '" + DropDownList1.SelectedValue + "',Position = '" + DropDownList2.SelectedValue + "',Roles = '" + TextBox1.Text + "',Status = '" + DropDownList3.SelectedValue + "',DispatcherCovering = '" + DropDownList4.SelectedValue + "' where Name='" + DropDownList1.SelectedValue + "'", con);
        comm.ExecuteNonQuery();
        con.Close();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
        ClearAllData();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("Delete Dispatcher_Roles Where Name='" + DropDownList1.SelectedValue + "'", con);
        comm.ExecuteNonQuery();
        con.Close();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Deleted');", true);
        ClearAllData();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True");
        con.Open();
        SqlCommand comm = new SqlCommand("select * from Dispatcher_Roles where Name= '" + DropDownList5.SelectedValue + "'", con);
        SqlDataReader r = comm.ExecuteReader();
        while (r.Read())
        {
            DropDownList1.SelectedValue = r.GetValue(1).ToString();
            DropDownList2.SelectedValue = r.GetValue(2).ToString();
            TextBox1.Text = r.GetValue(3).ToString();
            DropDownList3.SelectedValue = r.GetValue(4).ToString();
            DropDownList4.SelectedValue = r.GetValue(5).ToString();
        }
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        ClearAllData();
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

Solution

  • The easiest way to solve such issues is to use what is called "human engineering". In other words, you don't try to fix the software issue, but in fact change the workflow and how you present the data editing to the end user.

    For example, let's assume that your users during data entry are OFTEN entering a customer again (duplicate data entry).

    You can solve this issue by writing a bunch of code at save time, and after the user has finished all that work of entering a new customer, ONLY to find that the customer already exists?

    Well, since a user can't edit a customer, or a invoice or do ANY useful work without FIRST doing a search? Then the "human engineering" solution is to ALWAYS start the users on a VERY nice search page.

    Then make and START the users on a REALLY good/nice/easy/fast search page.

    They will then simple type in a few chars and find the customer first, (you display some search results). They then click on the customer, edit and hit save, and NOW are back to do battle with the next customer/task.

    Turns out, the above also facilitates your refresh issue, since WHEN they save, you simple return back to the search page so they can THEN work/find on the next customer.

    In some cases, the above may well cost an extra keystroke, or a extra mouse click, but this solves the searching to find existing data, and it then also tends to solve the issue of refresh issues, since during data entry, if they hit refresh without a save, then the data not been saved anyway.

    I tend to like this ui then:

    enter image description here

    In above, I don't have the refresh issue to EVER worry about a refresh, since I pop a dialog to edit the one record, and thus don't have to "empty" the records.

    Often, a better solution to these types of issues is to "human" engineer the interface so a refresh NEVER a problem in the first place.

    However, what you can do? When the user hits the save button? For the last line of your save code, do a response.Redirect("to same page") to force a re-load of the page. (you have a post-back + round trip anyway!). The result will be a blank new record, and one that will NOT result in a new adding of record if the user were to hit refresh button again.

    In other words, in any software the users can't do ANY useful work unless they first find and search and bring up that record.