Solved

ASP.NET Preventing Table insert after clicking on page refresh

Posted on 2009-04-14
10
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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