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. auto generate code number

auto generate code number

Scheduled Pinned Locked Moved C#
databasehelp
22 Posts 7 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.
  • F FEMDEV

    Hi , I need to create a code number automatically for the name entered. If the name is already used then add a number at it's end like 1 or 2 depending upon the highest value available in the database. something like namexx where xx is the highest number plus 1. please help it's very urgent.

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #2

    And what have you tried? Or since it's very urgent you expect someone to write it for you?


    I know the language. I've read a book. - _Madmatt

    F 1 Reply Last reply
    0
    • N Not Active

      And what have you tried? Or since it's very urgent you expect someone to write it for you?


      I know the language. I've read a book. - _Madmatt

      F Offline
      F Offline
      FEMDEV
      wrote on last edited by
      #3

      i tried to put the string of the latest code in an array and parse it for int. put the integers in a integer array. then convert them to form a string of int values and add 1 to this string converted to int. but i couldn't split the initial string!!! what should be the split criteria? string would be always of form fname.lnamexxx where x could be any number 1, 99,999,9999 anything. don't need anyone to write a code just an immediate help, if possible for split criteria of the initial string[fname.lnamexx] .

      N realJSOPR 2 Replies Last reply
      0
      • F FEMDEV

        i tried to put the string of the latest code in an array and parse it for int. put the integers in a integer array. then convert them to form a string of int values and add 1 to this string converted to int. but i couldn't split the initial string!!! what should be the split criteria? string would be always of form fname.lnamexxx where x could be any number 1, 99,999,9999 anything. don't need anyone to write a code just an immediate help, if possible for split criteria of the initial string[fname.lnamexx] .

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #4

        You don't need to parse the ints or use an array. Assuming the names are in a datatable, something like this

        int x = 1;
        string name = // remove numbers to get base name
        while(datatable.select('name = ' + name).count() != 0)
        {
        name = name + (x++);
        }


        I know the language. I've read a book. - _Madmatt

        F 2 Replies Last reply
        0
        • F FEMDEV

          Hi , I need to create a code number automatically for the name entered. If the name is already used then add a number at it's end like 1 or 2 depending upon the highest value available in the database. something like namexx where xx is the highest number plus 1. please help it's very urgent.

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

          so, add a table "known_names" to the database, two fields: name, occurence_count (do not try to reuse an existing table holding actual people!). when adding a new name, check "known_names" for existence; if it isn't there, add it with occurence_count=0, and use codedName=name if it is there, increment its occurence_count, and use codedName=name+occurente_count.ToString() :)

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


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          F 1 Reply Last reply
          0
          • N Not Active

            You don't need to parse the ints or use an array. Assuming the names are in a datatable, something like this

            int x = 1;
            string name = // remove numbers to get base name
            while(datatable.select('name = ' + name).count() != 0)
            {
            name = name + (x++);
            }


            I know the language. I've read a book. - _Madmatt

            F Offline
            F Offline
            FEMDEV
            wrote on last edited by
            #6

            oh..uh.. ..this is cool Y did i not think this...grr Thanks, Mark

            1 Reply Last reply
            0
            • L Luc Pattyn

              so, add a table "known_names" to the database, two fields: name, occurence_count (do not try to reuse an existing table holding actual people!). when adding a new name, check "known_names" for existence; if it isn't there, add it with occurence_count=0, and use codedName=name if it is there, increment its occurence_count, and use codedName=name+occurente_count.ToString() :)

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


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              F Offline
              F Offline
              FEMDEV
              wrote on last edited by
              #7

              Thanks Luc, This was the first thing that I thought as well, but I am not supposed to make changes to db model at this later stage in project.

              L 1 Reply Last reply
              0
              • F FEMDEV

                Thanks Luc, This was the first thing that I thought as well, but I am not supposed to make changes to db model at this later stage in project.

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

                then the only way left is to scan the existing coded names, find the most recent one, and "increment" it. I once did an IncrementString() method, which basically scans a string right-to-left for digits; replaces '0' by '1', or '1' by '2', or...; up to '9' by '0' which sets a carry, meaning you have to repeat the process for the next (R-to-L) digit. This way, there is no need to explicitly extract the number. It does rely on having some other means of finding the most recent coded name though. :)

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


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                F 1 Reply Last reply
                0
                • N Not Active

                  You don't need to parse the ints or use an array. Assuming the names are in a datatable, something like this

                  int x = 1;
                  string name = // remove numbers to get base name
                  while(datatable.select('name = ' + name).count() != 0)
                  {
                  name = name + (x++);
                  }


                  I know the language. I've read a book. - _Madmatt

                  F Offline
                  F Offline
                  FEMDEV
                  wrote on last edited by
                  #9

                  OH.. this will not work if the code is deleted in between then the count and max number at end will be an issue

                  N 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    then the only way left is to scan the existing coded names, find the most recent one, and "increment" it. I once did an IncrementString() method, which basically scans a string right-to-left for digits; replaces '0' by '1', or '1' by '2', or...; up to '9' by '0' which sets a carry, meaning you have to repeat the process for the next (R-to-L) digit. This way, there is no need to explicitly extract the number. It does rely on having some other means of finding the most recent coded name though. :)

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


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    F Offline
                    F Offline
                    FEMDEV
                    wrote on last edited by
                    #10

                    i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

                    H OriginalGriffO D 3 Replies Last reply
                    0
                    • F FEMDEV

                      OH.. this will not work if the code is deleted in between then the count and max number at end will be an issue

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #11

                      :wtf: I have no idea what you are talking about. :wtf:


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • F FEMDEV

                        i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #12

                        FEMDEV wrote:

                        stuck with how to pass a string as seperator in split function.

                        It may not be necessary to do this. According to the MSDN documentation for Int32.TryParse()[^]

                        When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

                        . I didn't think that it worked like that, however, it says that it does. From the int returned you will know how many characters to use in a String.Replace() call to insert the new value.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        L 1 Reply Last reply
                        0
                        • H Henry Minute

                          FEMDEV wrote:

                          stuck with how to pass a string as seperator in split function.

                          It may not be necessary to do this. According to the MSDN documentation for Int32.TryParse()[^]

                          When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

                          . I didn't think that it worked like that, however, it says that it does. From the int returned you will know how many characters to use in a String.Replace() call to insert the new value.

                          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

                          Sorry Henry, it does not work as the text seems to suggest. The string has to contain a number, nothing but a number, that is. int.Parse("123abc") does throw a FormatException, int.TryParse("123abc", out i) does return false. ditto for "abc123". :)

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


                          I only read formatted code with indentation, so please use PRE tags for code snippets.


                          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                          H 1 Reply Last reply
                          0
                          • F FEMDEV

                            i was trying to do something like this as well.. stuck with how to pass a string as seperator in split function..!! frustrating syntax error

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

                            Use a regex: "(?[a-zA-Z]+)(?\d*)" splits it into the Name and any number.

                            private Regex splitNameAndNumber = new Regex(
                            "(?[a-zA-Z]+)(?\\d*)",
                            RegexOptions.ExplicitCapture
                            | RegexOptions.IgnorePatternWhitespace
                            | RegexOptions.Compiled
                            );

                            I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

                            "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

                            L 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Sorry Henry, it does not work as the text seems to suggest. The string has to contain a number, nothing but a number, that is. int.Parse("123abc") does throw a FormatException, int.TryParse("123abc", out i) does return false. ditto for "abc123". :)

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


                              I only read formatted code with indentation, so please use PRE tags for code snippets.


                              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                              H Offline
                              H Offline
                              Henry Minute
                              wrote on last edited by
                              #15

                              I thought that was how it worked. Couldn't test it as the machine I'm using at the moment doesn't have VS on it.

                              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                              L 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Use a regex: "(?[a-zA-Z]+)(?\d*)" splits it into the Name and any number.

                                private Regex splitNameAndNumber = new Regex(
                                "(?[a-zA-Z]+)(?\\d*)",
                                RegexOptions.ExplicitCapture
                                | RegexOptions.IgnorePatternWhitespace
                                | RegexOptions.Compiled
                                );

                                I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

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

                                or use a new separator such as '#', or a fixed number of digits, or just locate the first digit using IndexOfAny(), or... :)

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


                                I only read formatted code with indentation, so please use PRE tags for code snippets.


                                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                                OriginalGriffO 1 Reply Last reply
                                0
                                • H Henry Minute

                                  I thought that was how it worked. Couldn't test it as the machine I'm using at the moment doesn't have VS on it.

                                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

                                  VS is the first thing I install on any PC I touch! And do you really need VS to test a single method? Notepad.exe + csc.exe are a mighty pair. :)

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


                                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                                  H 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    or use a new separator such as '#', or a fixed number of digits, or just locate the first digit using IndexOfAny(), or... :)

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


                                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


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

                                    You can tell .NET is a Microsoft product - a dozen different ways to do the same thing (none of them that obvious when you are getting started!) It took me ages to work out how to format a floppy in Win95, then I found about six in as many minutes...

                                    I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

                                    "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
                                    • L Luc Pattyn

                                      VS is the first thing I install on any PC I touch! And do you really need VS to test a single method? Notepad.exe + csc.exe are a mighty pair. :)

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


                                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                                      H Offline
                                      H Offline
                                      Henry Minute
                                      wrote on last edited by
                                      #19

                                      Shhhh! It's not my PC.

                                      Luc Pattyn wrote:

                                      Notepad.exe + csc.exe are a mighty pair

                                      If both were on this thing (and if I'd thought of it, which is doubtful) I might have. :)

                                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                      L 1 Reply Last reply
                                      0
                                      • H Henry Minute

                                        Shhhh! It's not my PC.

                                        Luc Pattyn wrote:

                                        Notepad.exe + csc.exe are a mighty pair

                                        If both were on this thing (and if I'd thought of it, which is doubtful) I might have. :)

                                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

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

                                        Notepad should be there, unless it got removed. csc.exe is part of .NET, of which some versions are always present on Vista/Win7 AFAIK. :)

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


                                        I only read formatted code with indentation, so please use PRE tags for code snippets.


                                        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                                        1 Reply Last reply
                                        0
                                        • F FEMDEV

                                          i tried to put the string of the latest code in an array and parse it for int. put the integers in a integer array. then convert them to form a string of int values and add 1 to this string converted to int. but i couldn't split the initial string!!! what should be the split criteria? string would be always of form fname.lnamexxx where x could be any number 1, 99,999,9999 anything. don't need anyone to write a code just an immediate help, if possible for split criteria of the initial string[fname.lnamexx] .

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

                                          Why don't you just have a identity column in your table. At that point, how many instances of a name (or any column value for that matter) you have is inconsequential...

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