How to generate Random ID in C#?

Page 1 of 1

6 Replies - 1145 Views - Last Post: 04 December 2009 - 12:00 PM Rate Topic: -----

#1 abhjos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-May 06

How to generate Random ID in C#?

Post icon  Posted 04 December 2009 - 06:54 AM

Hello friends,

I would like to get help in generating random Order ID using C#. The ID should be generated and then saved in the SQL Server database. Please help me with the complete code.

Thank you,
abhay

Is This A Good Question/Topic? 0
  • +

Replies To: How to generate Random ID in C#?

#2 kstr  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 60
  • Joined: 19-October 09

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 07:13 AM

View Postabhjos, on 4 Dec, 2009 - 05:54 AM, said:

Hello friends,

I would like to get help in generating random Order ID using C#. The ID should be generated and then saved in the SQL Server database. Please help me with the complete code.

Thank you,
abhay



Are you sure you want to generate a random ID? most of the time ID's are just auto-increment...
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 07:14 AM

View Postabhjos, on 4 Dec, 2009 - 05:54 AM, said:

Hello friends,

I would like to get help in generating random Order ID using C#. The ID should be generated and then saved in the SQL Server database. Please help me with the complete code.

Thank you,
abhay


It is a very bad thing to self generate a random id and then store it into you database as order id. There is always possibility that the function would produce the same random number and it is impossible to store two equal numbers in your database table as an id if this id is set as primary key. Why not let you database create it's own auto incremented number for each order id that is inserted in your Orders table?

Solution: Create int or some other numeric data type field in your Orders table in database, named ID. Then set this field IsIdentity property on true and set this field as primary key. Then you don't have to deal with generating a random number for your ID.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 07:23 AM

If you're dead set on using a random identifier then use the System.Guid class to generate a GUID.
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 07:33 AM

Microsoft SQL database has the ability to autoincremet the value so you really should use this.

generating a GUID is also a solution. Bat again, it is also possible to generate two identical GUIDs and try to store it in database as order id. Especially if thi order table will contain a lot of records.

Here is another one. Order id could be a string for example:

currentDateTime+RandomNumber

This would be much safer.
Was This Post Helpful? 0
  • +
  • -

#6 abhjos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-May 06

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 11:53 AM

View PostFlashM, on 4 Dec, 2009 - 06:33 AM, said:

Microsoft SQL database has the ability to autoincremet the value so you really should use this.

generating a GUID is also a solution. Bat again, it is also possible to generate two identical GUIDs and try to store it in database as order id. Especially if thi order table will contain a lot of records.

Here is another one. Order id could be a string for example:

currentDateTime+RandomNumber

This would be much safer.


Thank you for the reply,

I am working on the project which need to accept the new Order and then save the order in the DB and once the data is saved the email needs to send to the user with the new Order ID and for that reason I want to generate random ID, is it possible using C#, is so can you help me with the complete proper code.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: How to generate Random ID in C#?

Posted 04 December 2009 - 12:00 PM

No, show some effort on your part. You've been given plenty of hints and suggestions. DON'T just ask for code.

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1