Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying prevent duplicate entry when clicking browser's refresh button for this I am using this code.
VB
Sub Page_Load (sender As Object, e As EventArgs)
    If Not Page.IsPostBack
        Session("update") = Server.URLEncode(System.DateTime.Now.ToString())
    End If
End Sub

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

Sub Button1_Click(sender As Object, e As EventArgs)
    If Session("update").ToString() = ViewState("update").ToString() Then
        If AddEmployee(firstName.Text, lastName.Text) = 0
            Message.Text = "Success"
            Session("update")= Server.URLEncode(System.DateTime.Now.ToString())
        Else
            Message.Text = "Failure"
        End If
    Else
        Message.Text = "Failure - Session" 
   End If
    firstName.Text =""
    lastName.Text = ""
End Sub

It is working fine for that,but when I am trying to insert a new entry without refreshing page by browser's refresh or F5 ,a new data is not inserting.

So plz tell how to solve this problem.
Posted
Updated 21-May-10 21:09pm
v3
Comments
Ankur\m/ 22-May-10 5:49am    
Your code tells me that it would insert a record for the first time and after that it wouldn't insert at all. Is this the case?

Did you try debugging it? Is it that Session("update").ToString() = ViewState("update").ToString() condition is false and it is considered a refresh?

Debugging the code should tell you the issue. Check the values in Session and Viewstate and where they change.
As such, code looks fine to trap refresh event.
 
Share this answer
 
Instead of trying to detect a refresh, use a Response.Redirect(...) after saving. Then F5 will not post back.
See this Wikipedia article for more details http://en.wikipedia.org/wiki/Post/Redirect/Get[^].
 
Share this answer
 
I won't get notified if you answer my comment. So I am adding an answer.

Your code tells me that it would insert a record for the first time and after that it wouldn't insert at all. Is this the case?

I am saying this because. When the page loads for the first time, value of both ViewState and Session's "update" key gets the same value.
Now when you click the button, Page_Load code is not executed as it is inside Page.IsPostBack. Now Button1_Click is fired which will execute AddEmployee and
VB
Session("update")= Server.URLEncode(System.DateTime.Now.ToString())
will update the value of session.

Now, lets see what happens when you click the button again for entering a new record.
Firstly, the sequence of execution,
Page_Load then Button1_Click and then Page_PreRender.
Now, code within Page_Load is not executed (Page.IsPostback is true)
Button1_Click is fired. It would not go inside the loop as Session("update") is not equal to ViewState("update").

Which is what you don't want.

Please let me know if this is what is happening.
 
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