Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generate Sequential Number without database

Generate Sequential Number without database

Scheduled Pinned Locked Moved ASP.NET
helpquestioncsharpasp-netdatabase
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maikeru2000
    wrote on last edited by
    #1

    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.

    S D Y T A 5 Replies Last reply
    0
    • M Maikeru2000

      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.

      S Offline
      S Offline
      Stoffy1972
      wrote on last edited by
      #2

      If you can use a string, try System.Guid.NewGuid().ToString(). It's always unique. Read this[(MSDN)]

      Y 1 Reply Last reply
      0
      • M Maikeru2000

        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.

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Stoffy1972

          If you can use a string, try System.Guid.NewGuid().ToString(). It's always unique. Read this[(MSDN)]

          Y Offline
          Y Offline
          Yusuf
          wrote on last edited by
          #4

          But is that sequential? the OP mentioned sequential numbers.

          Yusuf May I help you?

          S 1 Reply Last reply
          0
          • Y Yusuf

            But is that sequential? the OP mentioned sequential numbers.

            Yusuf May I help you?

            S Offline
            S Offline
            Stoffy1972
            wrote on last edited by
            #5

            Sorry. Sequential - i did not recognize it. It's late at night :zzz:

            1 Reply Last reply
            0
            • M Maikeru2000

              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.

              Y Offline
              Y Offline
              Yusuf
              wrote on last edited by
              #6

              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?

              M 1 Reply Last reply
              0
              • M Maikeru2000

                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.

                T Offline
                T Offline
                T M Gray
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • T T M Gray

                  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.

                  M Offline
                  M Offline
                  Maikeru2000
                  wrote on last edited by
                  #8

                  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....

                  T 1 Reply Last reply
                  0
                  • M Maikeru2000

                    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....

                    T Offline
                    T Offline
                    T M Gray
                    wrote on last edited by
                    #9

                    Can you use that other system to generate the ID by creating an article stub?

                    1 Reply Last reply
                    0
                    • M Maikeru2000

                      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.

                      A Offline
                      A Offline
                      Adam R Harris
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • Y Yusuf

                        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?

                        M Offline
                        M Offline
                        Maikeru2000
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups