Solved

ASP.NET Preventing Table insert after clicking on page refresh

Posted on 2009-04-14
10
567 Views
Last Modified: 2012-05-06
I have a page that inserts a record into a table upon clicking submit, how can I prevent a duplicate insert from occurring if the user clicks on Refresh after clicking on submit?
0
Comment
Question by:ITHelper80
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24139683
Don`t you want to add primary key to your table???
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24139710
I am adding primary keys to the table upon submit....I dont want a duplicate record to be inserted, which is what is happening now if the user refreshes the page
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24139769
one way to do this is to have a status flag.So once submitted change the flag to false .Make it true only when you have new data.
So when updating,check for this flag and do the update.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24139822
then you must refresh session variables (if you have)  on page refresh
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24139842
http://www.gklein.org/tests/js/disablekey.html

function disableKey(event) {
  if (!event) event = window.event;
  if (!event) return;

  var keyCode = event.keyCode ? event.keyCode : event.charCode;

  //window.status = keyCode;
  //alert(keyCode);
 
  // keyCode for F% on Opera is 57349 ?!
 
  if (keyCode == 116) {
   window.status = "F5 key detected! Attempting to disabling default response.";
   window.setTimeout("window.status='';", 2000);

   // Standard DOM (Mozilla):
   if (event.preventDefault) event.preventDefault();

   //IE (exclude Opera with !event.preventDefault):
   if (document.all && window.event && !event.preventDefault) {
     event.cancelBubble = true;
     event.returnValue = false;
     event.keyCode = 0;
   }

   return false;
  }
}

function setEventListener(eventListener) {
  if (document.addEventListener) document.addEventListener('keypress', eventListener, true);
  else if (document.attachEvent) document.attachEvent('onkeydown', eventListener);
  else document.onkeydown = eventListener;
 
  if (!document.getElementById) return;
  var el = document.getElementById("Msg");
  if (el) el.innerHTML = "Event handler added.";
}

function unsetEventListener(eventListener) {
  if (document.removeEventListener) document.removeEventListener('keypress', eventListener, true);
  else if (document.detachEvent) document.detachEvent('onkeydown', eventListener);
  else document.onkeydown = null;
 
  if (!document.getElementById) return;
  var el = document.getElementById("Msg");
  if (el) el.innerHTML = "Event handler removed.";
}
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24140198
Right,typically thats what we do.As soon as the order is submitted we clean out the shopping cart.That way it avoids duplicate orders
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 24140481
Im not a javascript person so Gifts.... post is complete greek to me.....in any event I placed it into my aspx page and it didnt prevent the refresh from occuring.

Spprivate, can you give me an example of how you set the status flag?
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24140707
Declare a boolean IsUpdate = false
say wherever you build your productlist or cart have a global variable called
Set this flag to true
say in your btn_click event
before calling update method check
if(isupdate)
{
update
clear cart or productlist=""
isUpdate=false
}

Hope you got the idea
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24141111
Hi,

It is simple. Just copy the code to a javascript file and refer the file by <script type="text/javascript" src="<path>"> </script>

On the Submit button click event after you have updated the values into the database include the following code snippet.

Page.ClientScript.RegisterClientScriptBlock(btnSubmit.GetType(),"DisableKey", "setEventListener(disableKey)",true);
function disableKey(event) {
  if (!event) event = window.event;
  if (!event) return;
 
  var keyCode = event.keyCode ? event.keyCode : event.charCode;
 
  //window.status = keyCode;
  //alert(keyCode);
  
  // keyCode for F% on Opera is 57349 ?!
  
  if (keyCode == 116) {
   window.status = "F5 key detected! Attempting to disabling default response.";
   window.setTimeout("window.status='';", 2000);
 
   // Standard DOM (Mozilla):
   if (event.preventDefault) event.preventDefault();
 
   //IE (exclude Opera with !event.preventDefault):
   if (document.all && window.event && !event.preventDefault) {
     event.cancelBubble = true;
     event.returnValue = false;
     event.keyCode = 0;
   }
 
   return false;
  }
}
 
function setEventListener(eventListener) {
  if (document.addEventListener) document.addEventListener('keypress', eventListener, true);
  else if (document.attachEvent) document.attachEvent('onkeydown', eventListener);
  else document.onkeydown = eventListener;
}
 
function unsetEventListener(eventListener) {
  if (document.removeEventListener) document.removeEventListener('keypress', eventListener, true);
  else if (document.detachEvent) document.detachEvent('onkeydown', eventListener);
  else document.onkeydown = null;
}

Open in new window

0
 
LVL 6

Accepted Solution

by:
ITHelper80 earned 0 total points
ID: 24147703
Thanks for all the suggestions but I decided it would be easier to run a 2nd query to check for duplicates and then exit the Sub if the reader found a record.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question