page refresh automatically inserting duplicate records

Posted by Sanhtech under ASP.NET on 6/10/2013 | Points: 10 | Views : 3478 | Status : [Member] | Replies : 7
I have webform with grid-view and text fields form with insert ,update,delete buttons in same webform.

but when i refresh the page in browser it is creating duplicate records with uniq Id.
how to avoid page refresh from adding new records?




Responses

Posted by: Chvlsru on: 6/10/2013 [Member] Starter | Points: 25

Up
0
Down
Place the grid view in update panel then the data will be updated in grid view without page refresh and without any duplicates.make sure that your field in database has primary key....hope this will be helpfull..

Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvlsru on: 6/10/2013 [Member] Starter | Points: 25

Up
0
Down
I can provide you with a sample ..

inserting values..

protected void Button1_Click(object sender, EventArgs e)
{
string ID = TextBox2.Text;
string NAME = TextBox3.Text;
string COMMENTS = TextBox1.Text;
string QryString = "insert into test values('" + ID + "','" + NAME + "','" + COMMENTS + "')";
SqlConnection conn = new SqlConnection(_sruthiConnectionString);
SqlCommand CmdObj = new SqlCommand(QryString, conn);
conn.Open();
CmdObj.ExecuteNonQuery();
conn.Close();
this.GridView1.DataBind();
}

Design....

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None"

style="z-index: 1; left: 238px; top: 517px; position: absolute; height: 212px; width: 463px"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"
AllowPaging="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
<asp:BoundField DataField="COMMENTS" HeaderText="COMMENTS"
SortExpression="COMMENTS" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sanhtech on: 6/11/2013 [Member] Starter | Points: 25

Up
0
Down
when I enter details and click on insert button,record gets added in database.NOw if I am clicking refresh button then again same record is getting addded in db with next ID.

how to stop refresh button from adding records.?

Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvlsru on: 6/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
It seems that your problem is that if the user refreshes tha page, The same old values are updated in that database. This is happening because, the Post Back Buffer is having Old values and the server is not able to differentiate whether it is coming from Button click or page Refresh. You can find more details on this in the following link http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET


Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 6/11/2013 [Member] Starter | Points: 25

Up
0
Down
i think you have put your code in this...
protected void Page_Load(object sender, EventArgs e)
{

}
instead put in this if block.....
protected void Page_Load(object sender, EventArgs e)

{
if(!Page.IsPostBack)
{

}
}


Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sanhtech on: 6/13/2013 [Member] Starter | Points: 25

Up
0
Down
@Ankitsrist
No,I have code in submitbutton_click event.And If I add if(!Page.IsPostBack) before insert statement ,it is not inserting data even once.

what disadv if I add Response.Redirect(Request.Url.AbsoluteUri); in submitbutton_click event

Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 6/14/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Sanhtech,

As you said, you can use the below code:-

Response.Redirect(Request.Url.ToString(), false);

This will not effect you page. But Make sure to check IsPostBack properties on page load event.

Sanhtech, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response