Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a asp.net web form that has a textbox control and a button control.the buuton control has got a event handler.when the button gets clicked text from the textbox control is retrieved and then gets saved to the sql server database.the problem that i am facing here is when i click the button control the data gets saved to the database which is normal but when i hit the f5 key to refresh on the web browser the data gets saved again.
what's the solution to this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string data;
                
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            data = TextBox1.Text;
            using (SqlConnection con = new SqlConnection("initial catalog=users;data source=.; integrated security=sspi"))
            {
                if (TextBox1.Text != "")
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into dbo.names values(@data)", con);
                    SqlParameter dataparam = cmd.Parameters.Add("@data", System.Data.SqlDbType.VarChar);
                    dataparam.Value = data;
                    cmd.ExecuteNonQuery();
                    TextBox1.Text = "";           
                }           
            }
        }
    }
}
Posted
Updated 10-May-13 11:01am
v4
Comments
[no name] 10-May-13 18:39pm    
it would be bettr if you dont let your user refresh the save page. after saving the data to database redirect to other page where all the eata will be displayed. even if the user refresh this page nothing would go wrong

You must have kept same function i.e
C#
protected void Button1_Click(object sender, EventArgs e)

for both save and refresh buttons, go to the property of the refresh button change the function for mutton click event to some other name again define it.
 
Share this answer
 
Comments
Richard C Bishop 10-May-13 17:10pm    
I did not know mutton(sheep meat) had anything to do with programming. ;)
You can simpley add:

if(!Page.IsPostBack)
{
}

around the code you want to run when the button is clicked.
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

Session["Time"] = DateTime.Now.ToString();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["Time"] = Session["Time"];
}


protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Time"].ToString() == ViewState["Time"].ToString())
{
// Code for submitting data....


TextBox1.Text = null;

Session["Time"] = DateTime.Now.ToString();

}
else
{
// Code for page refresh....
TextBox1.Text = null;
Response.Write("Page Refreshed!");
}
}
}
}
 
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