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. General Programming
  3. C#
  4. Generate Sequencial Number

Generate Sequencial Number

Scheduled Pinned Locked Moved C#
csharphelp
10 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.
  • Z Offline
    Z Offline
    Zoomlion
    wrote on last edited by
    #1

    Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

    OriginalGriffO N L realJSOPR 5 Replies Last reply
    0
    • Z Zoomlion

      Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      So do it - what is the problem?

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      Z 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        So do it - what is the problem?

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        Z Offline
        Z Offline
        Zoomlion
        wrote on last edited by
        #3

        I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub

        OriginalGriffO L 2 Replies Last reply
        0
        • Z Zoomlion

          Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

          N Offline
          N Offline
          nagendrathecoder
          wrote on last edited by
          #4

          You just need to concatenate all strings, "your datetime string" + "000000" + your value of x. Are you facing any problem in this?

          1 Reply Last reply
          0
          • Z Zoomlion

            Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Ya what is the issue? Where you stuck ??

            Jinal Desai - LIVE Experience is mother of sage....

            1 Reply Last reply
            0
            • Z Zoomlion

              Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Following code will help you to achieve what you want in C#.

              string temp="0000000";
              int i=10;
              string finalstring=DateTime.Today.ToString("ddMMyyyy") + temp.Substring(0, temp.Length - i.ToString().Length) + i.ToString();

              Hope this will help!

              Jinal Desai - LIVE Experience is mother of sage....

              1 Reply Last reply
              0
              • Z Zoomlion

                Guys, I am using C# with MSS 2005. I need to generate a sequential number for a field (ItemNo) in the format ddmmyyyy000000x where x is a sequencial number eg (020520100000001,020520100000002, 020520100000003 ...) for records generated on May 2, 2010. Can any one help in this regard.

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                So create a static int variable (I would do this in a global static class), and increment it from anywhere in the code.

                public static Globals
                {
                public static int counter = 0;
                }

                Then, when you create your string, do it this way:

                Globals.counter++;
                string id = string.Format("{0}{1:0000000}", myDate.ToString("ddMMyy"), Globals.counter);

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                1 Reply Last reply
                0
                • Z Zoomlion

                  I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  So, what you are doing is: 1) Read the last record (presumably for this month) from your database. 2) If it doesn't exist, use a default of 1. 3) If it does, strip out the last 6 characters, convert to int, add one to it and use that. 4) Reattach that to the code for this month. Ignoring that you seem to have a fetish over naming your variables after months of the year, and the "str" prefix is a bit of a waste of time, since half way through you hold integers in them. What is the bit that is giving you problems? Apart from the whole approach, which is frankly rubbish? If you must have an item number that is a string, with a date and a sequence number, then: 1) Read the last record. 2) Assume none: set integer default to 0 in variable called nextSequenceNumber 3) If record exists, extract last 6 characters, and parse to int. Store in nextSequenceNumber. 4) Return date code + (nextSequenceNumber + 1).ToString("000000") Assume the database will contain corrupt data - use TryParse rather than parse, and log any failures. Try to deal with them nicely.

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • Z Zoomlion

                    I have something similar in VB6 with MDB but have difficulty using C# and MSS. See my VB6 code below and advice on how to produce something similar in C#. Public Sub GenerateCode(ByRef Coder As String) Dim strMar As String, intApr As Integer Dim strJul As String Coder = UCase(Coder) Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection On Error GoTo AddNewErr cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Manager.mdb" rs.Open "select ItemNo from Items where left(ItemNo,8) like '" + Coder + "' order by ItemNo asc", cn, adOpenKeyset, adLockOptimistic If rs.RecordCount <> 0 Then rs.MoveLast If Left(Trim(rs.Fields!ItemNo), 8) = Coder Then strMar = Right(Trim(rs.Fields!ItemNo), 6) strMar = Val(strMar) + 1 strMar = CStr(strMar) If (6 - Len(strMar)) <> 0 Then strMar = String$(6 - Len(strMar), "00000") & strMar End If Else strMar = "000001" End If Else strMar = "000001" End If Coder = Coder & strMar Editor = Coder Set rs = Nothing Set cn = Nothing Exit Sub AddNewErr: MsgBox Err.Description, vbExclamation, "Error Report" End Sub

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    There is a lack of PRE tags, semi-colons and curly braces. These codez don't belong here. :(

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Prolific encyclopedia fixture proof-reader browser patron addict?
                    We all depend on the beast below.


                    OriginalGriffO 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      There is a lack of PRE tags, semi-colons and curly braces. These codez don't belong here. :(

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      PRE tags, semi-colons and curly braces are the least of the problems! Even by VB standards, that is some clumsy code...

                      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      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