I have an attribute that I just added to my database to hold confirmation numbers. The only problem is they are all null so I am getting errors. Thus I need a method to generate random confirmation numbers (no duplicates). The column entries have a max size of varchar(20). Any ideas how to do this? Thanks
Solution:
randNum = Replace(Guid.NewGuid().ToString(), "-", "")
randNum = randNum.Substring(0, 19)
Did a minute of research and came across these examples.
Random numbers in VB: Random integer in VB.NET
Random numbers in SQL Server: http://www.sql-server-helper.com/tips/generate-random-numbers.aspx
If you don’t want to use numbers that don’t mean anything (if you’re just filling columns to avoid errors), you can make your variable nullable on the .NET side so it can house a null value.
EDIT:
You can create a GUID, remove the dashes and cut it down to 20 characters which is still pretty unlikely to be repeated.
EDIT #2: If you’re using a stored procedure to perform these updates for new records/changing records (you should be), you can create the random number on the SQL side and return it as the OUTPUT variable. That will give you validation that the record was created/updated as well as giving you the confirmation to present on the .NET side.
Edit #3: myGuild.ToString().Replace(“-“,””).SubString(0,20) should do the trick.