Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 10-May-13 10:51am
rtz87865
Edited 10-May-13 12:01pm
v4
Comments
Lal Miah at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You must have kept same function i.e
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.
  Permalink  
Comments
richcb at 10-May-13 17:10pm
   
I did not know mutton(sheep meat) had anything to do with programming. ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can simpley add:
 
if(!Page.IsPostBack)
{
}
around the code you want to run when the button is clicked.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!");
}
}
}
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100