Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code i am using to generate a unique student number. It successfully generates the 4 digit random number, however when i refresh the page it generates a new one. This is inconvenient since i want to be able to identify a student with it.
How can i Make it so it doesnt change?

public string StudentNumber
{


get
{
const int length = 4;
var now = DateTime.Today;
string dobsufix = Dob.ToString().Substring(2, 2);
string enrollsuffix = now.Year.ToString().Substring(2, 2);

var chars = "0123456789";
var random = new Random();
var result = new string(
Enumerable.Repeat(chars, 4)
.Select(s => s[random.Next(s.Length)])
.ToArray());

string stdno = enrollsuffix + dobsufix + result;
return stdno;
}
set { }
}
Posted

1 solution

Store it in the Session, and check if it exists.
If it does, use it.
If it doesn't, generate the new value.

When you've finished with it, clear the session value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900