How to do Database changes after user exit from the website? - c#

i have a rental website and when someone wants to make an offer he has 7 min to pay, if he wont pay the offer will delete.
i have a timer on my form to check the time, and when the timer is on 0:00 and the user didn't pay his offer will delete.
MY question is how can i check if user log out? i mean user can exit from the site (by clicking X) and his session will end.
i want to delete his rent offer if user quit from the website.
Thanks for the helpers.

For this scenario, I don't think its a good idea to rely on browser events, such as onunload & onbeforeunload. User may have opened more than one tabs. So closing one tab will remove the offer. Furthermore, if the user click back button these events will be fired. So don't rely on browser events for this.
(But, if the user clicked on LogOut then you have enough information to delete the offer.)
Perhaps you can use following approach to handle your original problem:
When user create a new offer store these details in the database with two extra columns: OfferCreatedUtcDateTime and PaymentCompleted(which should be false).
If the user completed payment successfully, you can set PaymentCompleted to true.
Then you can use one of the following two options:
Option 1:
Create a windows service which will check above database columns. If the PaymentCompleted == false and OfferCreatedUtcDateTime + offer valid period > CurrentUtcDateTime then you can delete this offer.
Option 2:
As mentioned by #nvoigt in the answer, every time user search for a resource you can ignore or delete offers which satisfies the condition mentioned in Option 1.
Hope this helps.

First do not fulfill offers that are older than your timeout(7mins) I'm assuming that you have OfferCreatedDate timeStamp. Second create a job that will clean all unfulfilled and expired offers. Hope this helps

You cannot. Not reliably. The user will not send you a nice message when he does not do something.
You can program your site to send you a signal if something happens, but you need to know when something doesn't happen. And it can "not happen" in multiple ways, many of them not allowing a signal to be transmitted.
Just imagine your user's train goes into a tunnel or he kills his browser, his computer crashes or cell phone loses battery power. All events that happen daily and all of them will not notify you nicely. They cannot.
So what you need to do is figure out a way to delete all obsolete orders. Either on a timer in an independent service, or maybe before a user places any order. But you need do that in a place independent of the user playing nice with your frontend app.
One way of handling this would be to save the date and time of creation with every offer you give out. Every time you check available resources and create a new offer for a user, delete all offers that are older than your limit before giving out new offers, thereby freeing up the blocked resources.

What about not focusing on how to set the timer to 0 when user session end but check other users timer's when another user create one ?
Then you can still have the checking process for the connected user, when it goes to 0 it stopped but for the case the user close the windows or leave, when another user create a reservation you also and firstly check if there's timer still alive older than 7 minutes and you release them so the user currently doing a reservation can do this one that has just been set as available ?

Related

Difference between calling __doPostBack and Click Event of an asp button

I am using __doPostback of a button control and it will take some time to complete the db operations. I am using it in an update panel and I am getting an issue from IE that it will say
Stop running this script.
May I know what is the issue ?
I think it may be due to __doPostBack. Previously it is direclty calling the Click Event. Thanks in advance.
Check your User Agent string. This same thing happened to me one time and I realized it was because I was testing out some pages as "googlebot". The JavaScript that is generated depends on knowing what the user agent is.
From http://support.mozilla.com/tiki-view_forum_thread.php?locale=tr&comments_parentId=160492&forumId=1:
To reset your user agent string type about:config into the location
bar and press enter. This brings up a list of preferences. Enter
general.useragent into the filter box, this should show a few
preferences (probably 4 of them). If any have the status user set,
right-click on the preference and choose Reset
Avoiding the 'Script taking too long' (all browsers have some form or another of this) message in browsers is relatively simple. You just have to make sure the browser knows you have not created an endless loop or recursion. And the easiest way to do is is to just give the browser a breather in between long running tasks.
have a look at this solution
http://www.picnet.com.au/blogs/Guido/post/2010/03/04/How-to-prevent-Stop-running-this-script-message-in-browsers

Controlling browser forward/back functions in web application

I'm writing a web-based application for internal use within the business where I work. It's a fairly complex application, with a lot of forms that will allow the user to view and enter data, which once saved will be stored in a database.
One thing I'm anxious to avoid is allowing a situation to exist where a user might enter large amounts of data in the browser, and then (either deliberately or inadvertently) navigate off the page without saving the changes. To this end, I have already implemented an entry page which opens up a new browser window in which there are no navigation controls at all; only what is provided on the web pages themselves.
However, there are two potential ways in which a user could still lose data:
The browser Close button is still enabled, and a user could potentially lose work by clicking it inadvertently. I can probably live with this, as it falls at the extreme end of helping the user not to shoot himself in the foot.
In Internet Explorer (and, apparently, in Firefox) the Backspace button works like a Back button. I only discovered this accidentally, and have as yet been unable to find a simple way of stopping this behaviour. This is potentially a problem, as an inadvertent use of the Delete key (e.g. having positioned the cursor in a read-only textbox, or when the cursor isn't on any particular field in the page) will navigate off the page.
What I would like to do, as a minimum, is prevent Backspace from navigating off a page if that page has any user-writable fields on it and any of those fields have been changed by the user since the form was loaded. Ideally, I would like to disable this particular use of the Backspace key completely, while the user is logged into this web application. The two possible ways that I can think of, for achieving this, are: (1) clear the browser's history as each page is loaded, or (2) trap the Backspace key and only allow it to work if the cursor is positioned within a field whose text can be changed (e.g. a textbox).
Can anyone suggest how I could achieve either of these things? The solution needs to be programmatic, rather than something that has to be manually configured on every browser in the company.
Instead of blocking* functionality that your users have learned to expect in their daily activities at work and at home, why not work with it? Make the "back" button actually take them to the previous screen as expected, and use AJAX to silently save the form as they fill it out (say, every 5 or 10 seconds), so when they return to the form you can check to see if they already have partial, unsubmitted values saved and reload them.
This approach aligns with the realities of web-based applications and delights users if implemented well. An alert that says "you did something wrong" just frustrates users and makes them trust your application less. Remember - users almost never do the wrong thing. It's our applications that aren't aligned with usage.
* more like trying to block functionality. As you've discovered, people who designed the interwebs and web browsers never really intended for site developers to totally disable moving back and forward in the navigation history.
What about something like this? You can ask them if they are sure before they leave.
var changes = false;
window.onbeforeunload =
function()
{
if (changes)
{
var message = "Are you sure you want to navigate away from this page?\n\nYou still have unsaved changes.\n\nPress OK to continue or Cancel to stay on the current page.";
if (confirm(message)) return true;
else return false;
}
}
You should look at the Javascript's window.unload event.
This is fired when the use tries to leave the page. You can't totally stop them leaving the page, but you can give them a chance to cancel.
try this
window.onbeforeunload() {
return "Are you sure you want to navigate away?";
}

Auto logon windows at a specific time using c#

Auto logon windows at a specific time. How can i do this in c# ?
Any ideas please...
Purpose : I need to perform some tasks at a specific time in midnight. which needs the windows to be logged on.
Take a look at the Scheduled Task. On the task panel you un-check the option "only after the current user logs on". That's it.
Update
So to make this with a little more explanation:
This was your question:
Purpose : I need to perform some tasks at a specific time in midnight. which needs the windows to be logged on
So to get this to work, you have to split your task a little bit up. At first write your task that should be performed (a batch-script, a self-written application, a third-party application with a configuration file or some command line parameters, etc.).
If you got your task up and you can run it on a single finger tip (e.g. click on a lnk file on your desktop, enter a single command on the command-line, etc.), it's time to automate your task for a specific time schedule. For this purpose Microsoft already provided a powerful tool, called Scheduled Tasks. It is located in the Control Panel. Here you can define Tasks which should be started with dozens of options and time plans. Just step through the wizard and after that make a double click or right click - Properties on the created task. Now step through all the TabPages and take a close look on all the available options. Everything you need can be solved here.
Update 2
So after reading your comment (and a few other):
If my system is locked, then i need it to get unlocked at a point of time
My first question would be: What do you mean with locked?
There are two states in which a system can be locked:
The system is freshly started, no session is running and you need to provide a username and a password to get a session to run.
A user has already logged in and the system is currently locked (cause the user pressed WinL, the screen saver is configured to lock, etc.).
The difference between those two states is, in the first you need to provide a username and a password, in the second one you only need to provide the password only.
What both have in common is you have to press CtrlAltDel to get the needed Dialog and this is not possible from an application.
If you really need to log-on or unlock the screen at a specific time you should start a search about gina.dll and how to replace or enhance it so that you can send a message to it, to do whatever you like. But this dll replacement can't be done in C#. It has do be done in C/C++. And i forgot, that the gina.dll approach only works on XP. For Vista/Win7 they changed the log-on process and the procedure to intercept it, but a search for the provided keywords should reveal the needed informations.
look at this thread.
You can replace your app with userinit.exe.
it runs before Explorer.

Stumped. (C#, JSP)

So long story short, the company I work for, that is all about saving money, went from 1 service application, to 2 service web interfaces.
They're not secure, have no cert's, not even over SSL...yet, they have a timeout associated with each interface. Which then breaks the page you're viewing, and you have to refresh IE, and then proceed to login again, and reset your dropdowns etc...
So.
I've tried using a generic webbrowser interface in C# to have a foundation in which I can refresh the connection to the server with the page open, as to avert the timeout.
But, first attempt results in my app, going to the appropriate page, then opening a new IE window when a service ticket is opened, and there must be a lack of cached data, which results in the new IE window that opens, to revert back to the main page which prompts me to login.
The interfaces are Jsp, but I'm more familiar with c#...
Tried catching link clicks to navigate the destination in the current window, but it seems that the data you double click that prompts the new page to open, is merely a data in a table, and not technically a "link"....
I've brought this to the attention of management, but like most huge corporations, I know nothing, and they never have to deal with it, so as they say in every meeting, "silence means compliance"....
Any thoughts?
Probably not the right answer, but...
Session cookies?
Session cookies = fail..
No 'remember me' option...
I can however enter a link in each browser to "goto the home" section of each app, which each page is already at, so it keeps the connection alive, and doesn't navigate away from the current page (keeping the dropdowns etc that are set after login)
But still, the above problem exists where if I use all this in a C# browser, etc...
I told my boss first thing 'they can't tell me the timeout is for security!' heh
And I still didn't get a positive answer from 'well at 10 seconds per re-logon, per x amount of people = thousands of dollars a year spent on re-logging on...'
:D
Use ajax.
poll a server page in a small interval (a minute)
if it returns "timeouted=true" (or whatever convention you like), refresh the page with javascript.
include the ajax snippet on every page (using <jsp:include>, for example)

How do i lock an asp.net page from multi-user editing?

i have a page with a series of checkboxes that authenticated users can change. I need to make this page only editable by one person at a time. So if a user goes into it and edits one of the checkboxes, noone else can go into the page and change other checkboxes.
I thought about an edit page link and a readonly page link (all controls disabled), then set a database flag if user enters under edit mode, but my concern is i wouldn't know if the user changed something, then just x'd out of the browser/app, locking everyone else out.
This is an internal app to company. Has anybody done something like this?
Any ideas or thoughts or suggestions?
Thanks
We have this functionality on an older ASP app. The user will load data with some type of primary key. We put in a DB entry to "lock" that page. If they correctly move through the site, it will unlock the resources at that time.
Other users opening this page will receive indication that the page is locked and a read-only version is rendered.
It would be fairly trivial to code a unPageUnload AJAX call to reset the lock for browser closing. We don't find this to be much of an issue and old locks are just cleared by an evening process if more than 4 hours old.
Our situation is where the pages are tied to specific regions of data. If this is a general config screen, I think a more dynamic AJAX solution that pushed the updates back and pings for changes might make sense. You would have to decide if you want to disable changes from others after the first update is received or implement collision detection for the data.
Some type of hashing of the page data would probably make this easier to detect changes.
You do what you said, but add a client side timer which will ping the server and tell you they are still there. If you don't get a ping within x mins you could let a new user go into edit mode but perhaps warn them (or not).
What about letting all users edit this page and how your script check in for page updates? Just like SO does, while you are typing in an answer, an orange message appears above saying "At least one new answer has been posted". You could display something like "The page has been modified since you last opened it".
There was something like timer in ASP.NET AJAX. You could use that to talk to the server to send "IN EDIT" status updates. You can even go further. Say you send "LOCKOUT REQUEST" requests every 15 seconds asynchronously and you expect to receive the "LOCKOUT GRANTED" response from server. If the response hasn't been received, you disable all controls on the page until maybe the next request receives the confirmation (the previous message could have been lost in the network). This way, if one user closes the browser, the other won't have to wait many minutes or hours until they get the edit permission.
Essentially, you need a distributed implementation for a critical section concept. It maube a challenge to implement it over HTTP. But that's a very interesting challenge, isn't it?
If you're trying to prevent two users from updating a db record and over-writing each other, perhaps it would be easier to detect this than prevent it.
On strategy for this is to include a "version" field in the record, and save that in a hidden field when rendering the page.
Then you simply include that as a condition of your update (i.e. UPDATE ... WHERE ID = myID AND VERSION = myversion) - if your update returns 0 rows, you know that someone else modified the data, and you can then decide what to do - reload the new data, offer the user a chance to compare them, etc.
How about an alternative to an extended lock?
Since you appear to be manipulating relatively small amounts of data, it would be more polite to put an encoded version of original state of the data in a hidden form field (or a datestamp, though that's less reliable; a hash of the values would work for larger amounts of data). In a transaction, check the state of the database against the hidden form values; if the original record has changed since the user submitted the changes, you reject the update. If not, accept the update, and commit the transacation.
Another approach could be to have an Application variable that contained a map or dictionary of locked items.
So, when one user hits edit, add an entry to the AppVariable Map or Dictionary, with the Key set to the primary ID of the field being edited. Then for all further requests, when they change between records, do a check of the ID within the map and if its being edited, Toggle off any update buttons. If you want to do it AJAXy, add a timer and an UpdatePanel and poll to see when the lock is released, then refresh the page with the updated data and enable the update buttons again.
Or, as a slightly greater UI, allow the users to edit while waiting for the lock to release ( the Map item to be removed ), then when it is removed, compare the fields they have been working on, with the updated database values and allow them to overwrite/merge their changes.
The only real downside is, 1) You would need to create one Application level Dictionary or Map for each table that you want to lock/unlock. 2) If you get into a webfarm environment, it breaks and you would have to use a different system.
Does that make sense?

Categories