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. How do I Generate a Number in C#

How do I Generate a Number in C#

Scheduled Pinned Locked Moved C#
csharpquestion
14 Posts 9 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
    Mads115
    wrote on last edited by
    #1

    Hi I am creating a Student Registration Form in windows application, i want to generate a Registration Number to the Student after completing the Form. The Number should be unique. Thanks in advance

    L M R J 4 Replies Last reply
    0
    • M Mads115

      Hi I am creating a Student Registration Form in windows application, i want to generate a Registration Number to the Student after completing the Form. The Number should be unique. Thanks in advance

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

      If you want a unique number, how about using GUID. It's simple and unique.

      M 1 Reply Last reply
      0
      • L Lost User

        If you want a unique number, how about using GUID. It's simple and unique.

        M Offline
        M Offline
        Mads115
        wrote on last edited by
        #3

        Thank u for your answer................ I am not aware of GUID, Can u give idea how to use it

        L 1 Reply Last reply
        0
        • M Mads115

          Thank u for your answer................ I am not aware of GUID, Can u give idea how to use it

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

          string myGuid = Guid.NewGuid().ToString();

          "myGuid" is always unique.

          M 1 Reply Last reply
          0
          • L Lost User

            string myGuid = Guid.NewGuid().ToString();

            "myGuid" is always unique.

            M Offline
            M Offline
            Mads115
            wrote on last edited by
            #5

            HI I run the code with GUID the value is too long "2d89b36d-39fd-4ac7-9cb4-bf0d3768a1c6" I need to display and use only Eight Digits....... pls give some other idea

            L 2 Replies Last reply
            0
            • M Mads115

              HI I run the code with GUID the value is too long "2d89b36d-39fd-4ac7-9cb4-bf0d3768a1c6" I need to display and use only Eight Digits....... pls give some other idea

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

              How about just increasing the number: Student 1 : 00000001 Student 2 : 00000002 ... Student 99999999 : 99999999

              1 Reply Last reply
              0
              • M Mads115

                Hi I am creating a Student Registration Form in windows application, i want to generate a Registration Number to the Student after completing the Form. The Number should be unique. Thanks in advance

                M Offline
                M Offline
                MumbleB
                wrote on last edited by
                #7

                I would suggest storing the number in a file outside your app and then read that number when processing a form. Once form has been processed just update the number to the next. This is how I currently do it to store version numbers for batch files generated from my apps.

                Excellence is doing ordinary things extraordinarily well.

                Y 1 Reply Last reply
                0
                • M Mads115

                  HI I run the code with GUID the value is too long "2d89b36d-39fd-4ac7-9cb4-bf0d3768a1c6" I need to display and use only Eight Digits....... pls give some other idea

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

                  If you want to add a new student, just get the record from database. For example, the last record is number "10". Then just give number "11" to the new student. It's so easy. The right way is to add a number automatically in database. It increase the number automatically and the number is a primary key. That means the number is unique.

                  1 Reply Last reply
                  0
                  • M MumbleB

                    I would suggest storing the number in a file outside your app and then read that number when processing a form. Once form has been processed just update the number to the next. This is how I currently do it to store version numbers for batch files generated from my apps.

                    Excellence is doing ordinary things extraordinarily well.

                    Y Offline
                    Y Offline
                    yesu prakash
                    wrote on last edited by
                    #9

                    int i = 1 . . . i = i+1

                    P 1 Reply Last reply
                    0
                    • M Mads115

                      Hi I am creating a Student Registration Form in windows application, i want to generate a Registration Number to the Student after completing the Form. The Number should be unique. Thanks in advance

                      R Offline
                      R Offline
                      Rob Philpott
                      wrote on last edited by
                      #10

                      Are you storing the information in a database? If so, get the database to create the id for you. Make the primary key (the registration number) an identity column and your registration number will be generated upon insertion to the database. If you storing the information in some other manner, you'll need to just build a counter to increase the registration number by some amount (could be 1, could be more in order to validate a number as a valid registration number and avoid typos) each time. If this is a serious application then you'll need to make sure you do this in a common place (singleton) and that this action is threadsafe.

                      Regards, Rob Philpott.

                      1 Reply Last reply
                      0
                      • Y yesu prakash

                        int i = 1 . . . i = i+1

                        P Offline
                        P Offline
                        padmanabhan N
                        wrote on last edited by
                        #11

                        if you close and open it again, then it will start from 1. so it wont work ypki! (for this we have to save it in view state)

                        Padmanabhan

                        1 Reply Last reply
                        0
                        • M Mads115

                          Hi I am creating a Student Registration Form in windows application, i want to generate a Registration Number to the Student after completing the Form. The Number should be unique. Thanks in advance

                          J Offline
                          J Offline
                          jaypatel512
                          wrote on last edited by
                          #12

                          Rob Philtop is correct.

                          J 1 Reply Last reply
                          0
                          • J jaypatel512

                            Rob Philtop is correct.

                            J Offline
                            J Offline
                            J4amieC
                            wrote on last edited by
                            #13

                            Bob Hilltop? Who's he?

                            H 1 Reply Last reply
                            0
                            • J J4amieC

                              Bob Hilltop? Who's he?

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

                              J4amieC wrote:

                              Bob Hilltop? Who's he?

                              It's not a who it's a where. It's the location of his secret lair.

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

                              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