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
How to generate Random ID in C#?
Page 1 of 16 Replies - 994 Views - Last Post: 04 December 2009 - 12:00 PM
Replies To: How to generate Random ID in C#?
#2
Re: How to generate Random ID in C#?
Posted 04 December 2009 - 07:13 AM
abhjos, 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
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...
#3
Re: How to generate Random ID in C#?
Posted 04 December 2009 - 07:14 AM
abhjos, 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
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.
#4
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.
#5
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.
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.
#6
Re: How to generate Random ID in C#?
Posted 04 December 2009 - 11:53 AM
FlashM, 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.
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.
#7
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.
Closed.
Page 1 of 1
|
|

New Topic/Question
This topic is locked



MultiQuote




|