Generate Sequential Number without database
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
Don't overlook the power of an Access database. (.mdb) In access you have a field type which is "Autonumber" In your website you might want to collect the following: Date Time that number was requested User who requested the number Working title of the article Your key to this table could be the Autonumber field. Very easy to hook something like this up. Should be about 1 day of effort. Access is perfect for this type of application.
-
But is that sequential? the OP mentioned sequential numbers.
Yusuf May I help you?
-
But is that sequential? the OP mentioned sequential numbers.
Yusuf May I help you?
Sorry. Sequential - i did not recognize it. It's late at night :zzz:
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
why can't you slap some simple static class to do the job for you.
public static class SequentialNumbers
{
private static Object obj = new Object();
private static UInt16 lastNumber = UInt16.MinValue;public static UInt16 GetNext() { lock (obj) { return ++lastNumber; } }
}
Yusuf May I help you?
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
What does your tool do if that is the only thing it would use a database for? You don't track which authors contributed to an article? You don't have the titles stored separately for convenient listing? DateTime.Now.Ticks would give you a reasonably unique ordered but non-sequential ID.
-
What does your tool do if that is the only thing it would use a database for? You don't track which authors contributed to an article? You don't have the titles stored separately for convenient listing? DateTime.Now.Ticks would give you a reasonably unique ordered but non-sequential ID.
Yeah, I realize it sounds a bit odd. Basically, it's a kludge to make it easier to write articles that are stored in another product that is too unwieldy to use. So yes all that information does get stored but in a DB that I have no access to. Thus my app only cares about the act of creating the article and not what happens to it afterward....
-
Yeah, I realize it sounds a bit odd. Basically, it's a kludge to make it easier to write articles that are stored in another product that is too unwieldy to use. So yes all that information does get stored but in a DB that I have no access to. Thus my app only cares about the act of creating the article and not what happens to it afterward....
-
Hello, I am working on a little tool to help multiple people write articles and I have hit a snag. Each article needs to have a unique, sequential id. This alone is not that difficult but I am wondering what the best way to implement it is as it seems like a waste to create a whole database just for tracking a "next id" field. I was thinking of using a flat file (text/xml?) that the asp.net page reads from and then writes the new number right away. However, in that case how do I control concurrency (i.e. what if one person is reading in the split second before another writes their number back)? Or is this really an issue in something this simple? Any advise on an appropriate method for keeping a "next id" information would be greatly appreciated.
Well if all you need is a simple way to do this you might try loading the last id from a flat text file or xml in Application_Start throw that into the application state and then in Application_End save it to the database. This way you dont have to worry about concurrency issues because the id is stored in memory and is only written back to the file when the application ends.
/* Global.asax */ void Application_Start(object sender, EventArgs e) { int id; int.TryParse(System.IO.File.ReadAllText(Server.MapPath("~/nextid.txt")), out id); Application["ID"] = id; } void Application_End(object sender, EventArgs e) { // Delete the old file System.IO.File.Delete(Server.MapPath("~/nextid.txt")); // Write out the new one System.IO.File.WriteAllLines(Server.MapPath("~/nextid.txt"), new string[] { Application["ID"].ToString() }); } /* Some publicly accessible class */ public int GetNextID() { // Increase the id Application["ID"] = Convert.ToInt32(Application["ID"]) + 1; return (int)Application["ID"]; }
Ofcourse you are going to want to add stuff like checking for the file before reading it and some error handling but im sure you get the point.If at first you don't succeed ... post it on The Code Project and Pray.
-
why can't you slap some simple static class to do the job for you.
public static class SequentialNumbers
{
private static Object obj = new Object();
private static UInt16 lastNumber = UInt16.MinValue;public static UInt16 GetNext() { lock (obj) { return ++lastNumber; } }
}
Yusuf May I help you?
While there were several good answers, I have decided to go with this one with the slight change that I will write the number to disk each time it's generated. I know that will limit performance to an extent due to IO but I think it is important for to maintain the Durability of the process (i.e. if it crashes at some point, someone doesn't have to scour through every article to find out what the last valid number was). A follow up question might be how to gently handle situations where people are waiting on the lock but I will try to find more information on this myself first before asking.