views:

92

answers:

3

I've received a rush project (asp.net c# framework v2), of course due on Monday morning. It's a very simple project -- add a "Request Quote" page to an existing site. Basically, collect some info and then email someone at the company the contents of the form, and show the user a "Thank You" page. Simple as pie...until I just read the last requirement.

Every form submission is supposed to have a "Confirmation Number", starting with 10000, and each one successively increments the number by one.

Sounds so easy right? Well, I don't have a database in this site. No idea why, I really don't know anything else about the site other than the info I need to fill the requirements.

So, with this in mind, and realizing that this is less than optimal, I guess my only solution is to make a text file (yuck) and read it in, get the last number used, add one, and write that back to the text file. Which of course leaves a lot to be desired in respect to "locking" of the file so I don't get duplicate numbers.

Anyone have any suggestions for me? I'm open to anything at this point...

A: 

I would think of XML as a good solution.

This post might make xml even easier for you.

Shimmy
In what possible way would XML be better than a plain text file?
Paul Tomblin
cuz xml is searchable, also in text you have to devlop a search/save engine or methods from scratch, in XML, Microsoft has already sweat for you...
Shimmy
Also, besides the fact that you can define IDs in xml, you can even use a strongly typed DataSet to serialize the data to and from the xml. I've done this before, it's just no-work.
Shimmy
Not really required for storing a single numeric value.
cxfx
But still easy to search for last value.
Shimmy
Shimmy, not a bad idea, but I don't think that would really help me as far as locking so I don't get duplicates. Unless I'm missing the point entirely, which is very possible.
Matt Dawdy
+2  A: 

First, I'm assuming each process has some sort of unique identifier available. This could be a process ID, or something that is guaranteed to be unique without race conditions. Write this value to a file with the same name, so that you don't have a race creating that file. Then, move it to a file called "lock" if it doesn't exist. Check that this happened successfully by looking at the contents. Now read the value from the Confirmation number file. Check that the "lock" file has your unique identifier in it -- this ensures there wasn't a race from two people trying to move their file to lock. If it is yours, write back the incremented number and your unique ID to the Confirmation number file. Check again you hold the lock, and that the Confirmation number file has your ID. Then remove the lock file. Should this fail, just sit around waiting for the lock file to disappear, then try again.

This should allow you write to a file in a race-free manner, and ensures that the sequence number always increases by one.

McPherrinM
Very interesting. Since I don't know (off hand) of any guaranteed unique thing I can get my hands on in asp.net for a request, maybe I could just generate a guid right then? I mean, this won't involve any postbacks from the client, at least during this process of getting a new identifier, so that should work, right?
Matt Dawdy
Generating a GUID would work, so long as it is actually unique. If it is selected randomly, there's an infinitessimely small chance that you'll get the same GUID during a race condition, and things will break.
McPherrinM
I was thinking of using Guid.NewGuid(). Wouldn't that work?
Matt Dawdy
That has a very, very low chance of having a collision, but it does exist. I think it would probably be a reasonable thing to do.
McPherrinM
Worked like a charm. The only issue I see is that if, for some reason, the code dies somewhere and I've my guid written in the lock file, there isn't anything that takes my guid out.Well, I think I'll send this back, as is now, with some suggestions about adding a new table and refactoring the code at that point. I abstracted away the confirmation number stuff into a function, so it should be easy enough to change that function's implementation without messing up anything else I did tonight.Thanks for the help.
Matt Dawdy
A: 

You could keep track of the confirmation number in the Windows Registry, but given the option it's probably easier to use a simple text file as you suggest. Make sure you clean up (i.e. Dispose) the file handles each time to release any file locks.

cxfx