Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET Preventing Table insert after clicking on page refresh

Posted on 2009-04-14
10
Medium Priority
?
593 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

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