hi,,
i have one field form.
after entering data in field the value was saved in grid.
but while refreshing(f5) the page again the same data inserted in to grid.
i want to rectify this problem/
help me
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted May 28, 2012, 3:11 PM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication16._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication16
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str ;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPageRefreshed)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "INSERT INTO test(name) VALUES(@name)";
com = new SqlCommand(str, con);
com.CommandType = CommandType.Text;
com.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text.Trim();
int i = com.ExecuteNonQuery();
lblMessage.Text = "Records inserted successfully";
bind();
Clear();
}
else
{
}
}
void bind()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from test";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "test");
GridView1.DataMember = "test";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
private void Clear()
{
TextBox1.Text = "";
}
bool IsPageRefreshed = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set the Postback ID
string postBackID = System.Guid.NewGuid().ToString();
ViewState["postBackID"] = postBackID;
Session["postBackID"] = ViewState["postBackID"].ToString();
}
else
{
string sessionPostBackID = Session["postBackID"].ToString();
string viewPostBackID = ViewState["postBackID"].ToString();
// If ViewState GUID is not equal to Session GUID, then Page has got Refreshed.
if (viewPostBackID != sessionPostBackID)
{
IsPageRefreshed = true;
}
// Set the Postback ID
string postBackID = System.Guid.NewGuid().ToString();
ViewState["postBackID"] = postBackID;
Session["postBackID"] = ViewState["postBackID"].ToString();
}
}
}
}
Thanks
If this post helps you mark it as answer