Click here to Skip to main content
11,928,919 members (55,356 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked

Avoid inserting twice when user refresh page in ASP.NET

, 28 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Avoid inserting twice when user refresh page in ASP.NET
Hi all,

This is something that I have done to avoid insertion of records into database twice when user presses refresh. It is so simple. I am just using session and view state, and comparing the dates stored in them.

Here is the code:

'Storing the now date in session variable on page load:
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

'Storing the date also in view state on page prerender :\
Sub Page_PreRender (sender As Object, e As EventArgs)
    ViewState("update") = Session("update")
End Sub

Finally check if the dates are equal, so if yes we insert and update the session value to the date of insertion, thus the view state value and session value will not be equal in the next postback. Here is the code :

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())
            Message.Text = "Failure"
        End If
        Message.Text = "Failure - Session"
    End If
    firstName.Text = ""
    lastName.Text = ""
End Sub

Hope that you like it.


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


About the Author

mhamad zarif
Software Developer Netiks
Lebanon Lebanon
I have been a programmer for about one year.My power is in and flash (actionscript 3).I also work on windows application using and c#.

You may also be interested in...

Comments and Discussions

GeneralRe: why would i want to learn - especially when it has su... Pin
guylwalker30-Aug-11 17:54
memberguylwalker30-Aug-11 17:54 
GeneralRe: doh, i should have known that! it's been forever since i di... Pin
guylwalker30-Aug-11 17:50
memberguylwalker30-Aug-11 17:50 
Generalvery nice article // Pin
dineshkummarc18-Nov-11 0:43
memberdineshkummarc18-Nov-11 0:43 
GeneralYou are using an assignment operator (=) in your two If stat... Pin
guylwalker29-Aug-11 13:21
memberguylwalker29-Aug-11 13:21 
GeneralRe: In VB.NET, (=) is the comparison operator and the assignment... Pin
AspDotNetDev29-Aug-11 14:14
protectorAspDotNetDev29-Aug-11 14:14 
GeneralRe: hey man.This is and not c#.In we use only sing... Pin
mhamad zarif29-Aug-11 23:26
membermhamad zarif29-Aug-11 23:26 
GeneralReason for my vote of 3 Netural. Just wondering why Server.U... Pin
Nikhil Mittal29-Aug-11 2:44
memberNikhil Mittal29-Aug-11 2:44 
GeneralI don't think Server.URLEncode really required here? Pin
Venkatesh Mookkan28-Aug-11 19:08
memberVenkatesh Mookkan28-Aug-11 19:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 28 Aug 2011
Article Copyright 2011 by mhamad zarif
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid