IsPostBack failing in ASP.NET? - c#

We have an ASP.NET 3.5 application which we have created for a logistics company, which allows their customers to input deliveries/jobs and get them onto the system.
The core page in the system is the Add Job screen as as you can imagine - as the user builds up their job, we store some information of this in the Session. Then when they commit the job, or in the Page_Load event when the request is not a postback, the session is cleared ready for the next job.
We are having an issue with a small percentage of the customers, in which the users sessions appear to be being wiped before the commit happens - the users have assured us they are not trying to use multiple sessions (i.e. 2 windows of the same page). We put in some error trapping that logged whenever the sessions were cleared on that page, and it seems that 'clear session' in the Page_Load event is firing a few seconds before they click submit. Please note we have a lot of autopostback controls on this page.
Are there any ASP.NET quirks, or browser quirks for that matter, that will mean the IsPostback property will incorrectly be false? This is a very sporadic issue and we are having no luck in replicating this!

As adviced by many comments the session IS the wrong solution. It could be reset by a lot of things (app pool recycling for example).
You should not force the user not to use different browsers or tabs, so the right choice is move data to ViewState/Control state.
If for some weird reason you still have to keep data in the session consider switching to a persistant session storage (i.e. using sql Server).

Related

ASP.Net C# Web Form: Prevent new server session while previous is processing

I have an ASP.NET 4.5 web form running C# code behind on a server. A specific form often takes some time to finish while it updates and changes various database records. However, if the user closes the tab or tries to reopen the web form, it will try to check the users status in the database and fail when those later change due to the first running process.
The need is to track this specific instance of the process and user, and if it is still running, prevent the page from loading fully or redirect. I was hoping to find and store some user and process information on a cookie and then simply check for this each time on page_load. I was not able to find these variables/properties.
Am I going about this the right way, and if so, how can I accomplish this?
Thanks!
I was not able to find the exact solution I was looking for. At the moment, I cannot see any way to find a server side identification id of the process.
Instead, I referenced How to tell if a page unload in ASP is a PostBack and made it so that the page would warn when being unloaded before the confirmation screen is shown. As long as the form opens to the same named tab, the user would be given the warning screen and given a confirmation before they could close or reload a new web form instance.

c# cache not persisting between users

I'm experiencing some problems with the use of System.Web.Caching.Cache.
Basically I'm using
Cache.Insert(tablename, newlist, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration);
to insert objects into the cache, and Cache[tablename] to retrieve them on a webform.
What I'm experiencing is that if I refresh the browser that calls these methods, then the data is there, and Cache.Count reflects this.
However, if I close the browser and start it up again, or start a different browser on my test machine or another, then the Cached data is not there.
Ie the Cache is working as if I was using the Session Store. I've also tried using the Application store too, but with exactly the same results.
I've had a look through my application pool settings and web.config, and can't see anything obvious.
Any help with this would be much appreciated, as requerying the databases to get the information to put in the cache would slow down the webform a great deal.
I'm not sure I'm being clear enough.
To reproduce.
browser 1 opens webpage, no cached data
browser 1 refreshes webpage, cached data
browser 2 opens webpage, no cached data
browser 2 refreshes webpage, cached data
browser 1 refreshes webpage, cached data
So as you can see, it's working exactly as if it was session only data, not a common cache. Using the callback probably wouldn't tell me anything, as the data is still there, just not available to a fresh session
Sounds like you restart the application by pressing F5 in Visual Studio.
Just open a new browser Window without restarting the application.

Why does my new .Net 4.0 EF application randomly lock up?

I have recently rolled out a new ASP .Net application. This application uses .Net 4.0 and the Entity Framework 4. It is a "basic" CRUD application. I almost exclusively used the EntityDataSource for Data Access.
The users have reported "ocassional freezing" of the application (they all use IE 8). If the user closes his browser and starts back up, the app is fine. It happens maybe once or twice per day total, usually to the heavy users. (So, I have been leaning toward memory leack or some other kind of resource limitation.)
Until just a little while ago, I have never seen this happen. It dosn't leave anything in the Event Log on the client PC nor Server. Also, there is nothing in the IIS error logs or the SQL logs.
A little while ago, I finally got it to happen. I was switching all of my EntityDataSources to use a single Object Context per page. I downloaded the EF Profiler (which seems to be a great product) and it pointed out that I am using multiple object contexts on many of my pages, due to the fact that many of them have more than one EntityDataSource. So, I was cleaning all this up and testing each page, when lo and behold....IE stopped loading pages!
At first, the page started loading slowly (and this was a page that I had already modified to use a single Object Context, so although I still believe I need to implement this "singleton" pattern throughout all my pages, I guess it isn't the culprit! Anyway..), then at some point I received:
Message: Sys.WebForms.PageRequestManagerTimeoutException: The server request timed out.
Still, nothing in the Event Log. I also checked the IIS web logs and I don't see anything, but they are pretty tough to read with the naked eye, I may have to download a parser to make sure there is nothing in there.
Anyway, I decided to start up another window and navigate to my app, and sure enough that window worked fine, no problems. I went back to the windows that stopped responding, refreshed it, waited... went to the home page of my app (which is basically just some static html.. and nothing.. ). Next, I opened a new tab in the window that is currently frozen and that tab works fine. So, I go back to the window that can no longer load pages from my app and try some other web sites.... espn.com loads fine, msn.com loads fine, I then enter the URL for my app home page (and tried some other pages in my app) and NOTHING. IIS doesn't even log the attempts in its Log Files (its like IE has just decided not to do anymore "gets" for my localhost!). Finally, after waiting for a while (while I was typing this post) I now have this error message in that window:
Internet Explorer cannot display the webpage. What you can try: The problem can be caused by a variety of issues, including: BLAH BLAH BLAH...
So, I jump back to that window and it can still browse Internet sites just fine. But, once again when I try to go to any of the pages in my app, I get NOTHING (spinning blue circle in the tab). So, once again I start a new tab in that same browser and I can browse around my application just fine.
What has my app done to this browser instance that will no longer let it load any of its pages (in fact, it seems like it isn't even doing a GET on any of the apps pages, since there is nothing new in the IIS logs...except for when I load the pages in these other tabs and/or browser windows that work fine, so the logging is still working...)? I will be forever indebted to the person that can help me figure this out. I am not sure it will do much good, but I am going to leave this browser window up on the chance that someone can tell me some things to try to diagnose this problem. It feel like I am THIS close to figuring it out. I finally have a window up that is misbehaving. I am afraid to close the window or do a new build on the app until I get some good input on things to try....
I should add that when this happened to me, I was running on my local PC, so I was the only user and only had one window open to the app..
Although, I am completely stumped!
We had the same issue and seemed to fix it by adding the following JavaScript to all pages with ajax:
window.onunload = abortRequest;
function abortRequest() {
Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
}
I hope this helps!
sounds like a request just takes too long to execute. A web-browser is only allowed to keep two concurrent connections to a single webserver unless you utilize http pipelining (which isn't well supported anyway). However, My experience is that this is per browser, not per tab.
This is why nothing shows up in your logs, because your browser never initiate a request because it is waiting for the page to finish loading before it can fetch another page. I would start looking into the last request initiated before the lockup, it is probably that page that locks for some reason and never finishes.
ASP.NET utilizes only one thread as long as all request are sequential. As soon as you fire up two request simultaneously, a new thread is initiated (Init() on HttpApplication is invoked), and that can cause intermittent errors if you do funky stuff on Init().
If you think this is related to EF, you might read up on how to avoid locking in multi-threaded environment (because a web application is very much multi-threaded however this usually kicks in a production environment).
Ugh, I feel your pain - I hate these types of issues. Here's my divide-n-conquer process when something like this is happening (adapted somewhat to your case):
Step 1 - Environment Visibility
Get yourself set up so you can roughly see inside your systems. Set up Performance Monitor, and add a couple of important counters (IIS Current Requests, ASP.NET Current Requests, SQL Server - Active Transactions). The idea here with the "active requests/transactions" counters is you want to find out which part of the system is holding on to the request.
Additionally, add counters for total request time in IIS and the DB (so you can see it climbing as something is hanging on).
On your client PC, set up Fiddler.
Step 2 - Reproduce Issue
Usually at this point, the application realizes you can see it and starts behaving perfectly. ;) Stop and make sure you can still reproduce the issue.
Step 3 - Take the DB out of the equation
Create 4 pages in your application:
Static HTML file (.htm) that will NOT go through the ASP.NET runtime engine. This is your baseline for raw IIS.
ASPX page with no tricks (no database access, no processing, just a simple page that ASP.NET can spit out)
ASPX page with one simple database call (of some size, maybe do a single big query or something)
ASPX page with some complex transactions (multiple queries, etc)
Step 4 - Reproduce and measure
Since this issue will probably be pretty obvious once you can see it, find the bottleneck with your performance counters and Fiddler. Your issue sounds like you could have an intermittent connectivity issue (either between client and web server or web server/SQL), especially if IE is giving you the "page cannot be displayed" error, but you should see that in Fiddler. On the server side, you should see one of those performance counters hanging.
well, sounds like the app is waiting for a database operation to complete but is locked, probably because of another operation.
Try using the SQL profiler to trace the activity, and isolate what processes are running for a long time.
You can differentiate the requests using the SPID column in the trace view, it contains a unique ID number for each session from your App.
I have new information regarding this issue.
The application uses Integrated Authentication. This causes every reqest to come back (twice) with access denied, before IE send the credentials.
I can't help but wonder if this is what could be causing the "time outs".
I implemented an Ajax "Loading" animation on every page that prevents the user from clicking a button until the previous request is completed and that has GREATLY diminished the amount of "freezes" the users are experiencing.
Although, it still happens somewhat often.

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