//This method generates UserID,Password and CitizenType based on validations and conditions mentioned in the SRD and insert in database public static UserRegistration Registration(UserRegistration R) { try { string citizentype = string.Empty; string userid = string.Empty; //UserID Generation if (R.ApplyType == "Passport") { int passid = (from c in P.UserRegistrations where c.ApplyType == R.ApplyType select c).Count() + 1; userid = R.ApplyType.Substring(0, 4).ToUpper() + "-" + string.Format("{0:0000}", passid); } else if (R.ApplyType == "Visa") { int visaid = (from c in P.UserRegistrations where c.ApplyType == R.ApplyType select c).Count() + 1; userid = R.ApplyType.Substring(0, 4).ToUpper() + "-" + string.Format("{0:0000}", visaid); } //Password Generation List <string> retrived_pass = (from c in P.UserRegistrations select c.Password.Substring(c.Password.Length - 3, c.Password.Length).ToString()).ToList(); string randomid = string.Format("{0:000}", random.Next(0, 999)); while (true) { if (retrived_pass.Contains(randomid)) { randomid = string.Format("{0:000}", random.Next(0, 999)); } else { break; } } char[] specialchar = { '#', '@', '$' }; char sp = specialchar[random.Next(0, specialchar.Length)]; DateTime today = DateTime.Today; string password = string.Format("{0:00}", today.Day) + today.ToString("MMM").ToLower() + sp + randomid; //CitizenType int age = (int)(DateTime.Today.Subtract(R.DateOfBirth).TotalDays / 365); if (age >= 0 && age < 1) { citizentype = "Infant"; } else if (age >= 1 && age < 10) { citizentype = "Children"; } else if (age >= 10 && age < 20) { citizentype = "Teen"; } else if (age >= 20 && age < 50) { citizentype = "Adult"; } else if (age >= 50) { citizentype = "Senior Citizen"; } //Inserting into Database R.UserID = userid; R.Password = password; R.CitizenType = citizentype; P.UserRegistrations.Add(R); P.SaveChanges(); } catch (Exception) {} return(R); }