Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Preventing Duplicates Insertion While Refresh the Page in ASP.net Web applications

March 9 2009 , Written by Balavardhan Published on #ASP.Net

How do I prevent previously submitted form data from being reinserted into the database when the user presses the browser's Refresh button?

I set up a simple ASP.NET page that collects first name and last name data and, upon the click of a submit button, inserts it into the Employees table database. The default behavior of such a page, without any special programming, is to submit the form data upon the button click, insert the data into the database, and bring the user back to the page with the first name and last name text boxes still populated. Pressing the form's submit button again will reinsert the same data into the database, as will pressing the browser's Refresh button.

First Way

Use Responce.Redirect to Same page.

Sub Button1_Click(sender As Object, e As EventArgs)
If AddEmployee(firstName.Text, lastName.Text) = 0
Message.Text = "Success"
Response.Redirect(Request.Url.ToString(), false) ' will include the querystring
Else
Message.Text = "Failure"
End If
firstName.Text = ""
lastName.Text = ""
End Sub


-----------------------------

Second Way

Using Session Value

Dim hiddenValue As String

Sub Page_Load (sender As Object, e As EventArgs)
If Not Page.IsPostBack
hiddenValue = "first"
End If
End Sub

Sub Page_PreRender(sender As Object, e As EventArgs)
Session("hiddenValue") = hiddenValue
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
If Session("hiddenValue") = "first" Then
If AddEmployee(firstName.Text, lastName.Text) = 0
Message.Text = "Success"
hiddenValue = "second"
Else
Message.Text = "Failure"
End If
Else
Message.Text = "Failure - Session"
End If
firstName.Text = ""
lastName.Text = ""
End Sub

-----------------------------

Share this post

Repost 0

Comment on this post