views:

763

answers:

7

I am developing a very small survey application, likely 3-4 pages web application. so my question is what is the best way to save survey data?

by the way so far I have come up with the following ways.

  1. save to database and mark each survey as incomplete. when user submits last form of the survey check completeness of the survey and mark it as complete if it is complete.

  2. save to sessions and than save to database when user submits last form of the survey.

what do you think about those methods?

A: 

If you want them to be able to save their results mid way through the survey and come back at another time to complete it, then you'll have to store the start time of the survey and the end time to know that they completed it. You'll also have to store all their values so far.

Since it is small, I would just keep it in session and then save it to the database upon their completion.

Nick DeVore
+3  A: 

I've had quite some headaches with this sort of thing and would advise against using Sessions if at all possible. When your site gets moderately heavy traffic, or if ASP.NET just feels like it, you may find you lose all your sessions as .NET tries to free up some memory.

I'd go for putting it in the database as soon as possible. Use Guid.NewGuid() to generate a unique unguessable key that you can pass from one page to the next in the query string: that'll be your key to retrieving the record from the database.

Two other options you haven't considered:

  1. Post the response from one page to the next. (This might be more hassle than it's worth.)

  2. Use the asp:Wizard control. That way your 3-4 pages will actually be 3-4 steps on a single page. The data from each stage is automatically stored in the ViewState for you, so the input fields from the first step will still be accessible to you when you're on the last step.

(I'd still go for your option number 1 though.)

teedyay
I will go for first option as it is more robust than the session. guid(unique identifier) good idea, I will add guid to the table as it will totally eliminate the need of sessions.Thanks
Mohamed
A: 

I think the second option would be your best bet. If your site receives heavy traffic, option 1 will put more stress on the database and also clutter the database with incomplete surveys. Sessions sounds like a better option because really, you don't want the data unless it's complete, do you?

John T
A: 

Both seem fine, and that will depend in what you expect your users to do or what you expect to your users to behave.

The first method will allow users to return back (even after closing the browser) to finish the survey. That's a good point, if your survey were big and users can't finish it before get lost in something else, but that's also a negative point if you want to force the users to finish, I mean, if you don't want incomplete surveys.

The second method forces the user to finish the survey at the same time they start. That's a good point if your survey is quick and you want complete data about them, but it's bad if your survey is long and normally the user gets distracted before finishing it.

I would say that the number of questions of each of pages will be defining factor to choose between the two methods for me, perhaps it can be for you too.

Augusto Radtke
+1  A: 

Do not use sessions for this. Microsoft has a fantastic example of how not to de surveys in their application they use at TechEd. Every time you have to think about an answer, the session times out. Use ajax, save each answer, even a partial one. If you run into DB performance problems, collect all answers for all surveys for a certain time, and save those together. With some javascript you can do a callback to the server only when something changed in the past few seconds, or before the window is closed.

Unless, of course, you have a time limit for answering the questions.

Stephan Eggermont
+2  A: 

If you anticipate that your application might ever grow in the future (providing longer surveys, maybe even in a form where users can same "in between" results and resume at a later time), I'd definitly go with the database option.

ISW
+1  A: 

Here is how I do it:

  • I generate the 'questions' from the database thus there is only one page used to show the different questions.
  • I save the answers to every question as soon as the user clicks 'next'. This has the big advantage that the user can come back and start from where she left.
  • I track progress with a pointer which is updated together with the saving of the result.
  • I don't use session because I don't want to risk loss of data and I don't want to strain memory.
  • When pointer == number of questions, mark as completed.
tharkun