?
Solved

ASP.NET Preventing Table insert after clicking on page refresh

Posted on 2009-04-14
10
Medium Priority
?
585 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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