I neet to generate customer id on clicking the submit button in a registration form which should also be stored along with customer details in SQL database.The problem is it generates for the first id 1 and then 2,after that it remains at customer id no 2 itself.and i want it in the following format : Current Year(4 digits)+Current Month(2 digits)+Number(4 digits), eg:2015120001 but I'm getting it as 2015121.. I have tried the following code to generate the id..pls check the link Code to generate the id
2
-
Never use All caps. It just hurts the eyes. – Shamas S Dec 8 '15 at 4:32
-
Sorry I'll see to it next time – sharamath Dec 8 '15 at 5:29
Add a comment
|
You need to store your counter in outside your method, like in SQL Server and then return the customer id. Below is a sample code:
private static readonly object o = new object();
public static int GenerateNextCustomerId() {
lock (o) {
// Write code to get the next counter from db in this method
var counter = GetNextCounter();
var nextCustomerId = string.Format("{0}{1}{2}",
DateTime.Now.Year, DateTime.Now.Month,
counter.ToString().PadLeft(4, '0'));
// Write code to increment the counter and save to db in this method
IncrementCounter();
return int.Parse(nextCustomerId);
}
}
-
I got the desired output, I have a clarification, the customer id is of string type here, but in my database its of int type. when insert statement is executed it will throw an error,can a conversion from string to int be done? – sharamath Dec 8 '15 at 9:18
-
I have made the change to code to return the customerId as int. All you need to do is to parse the customerId to int. – Ankit Vijay Dec 8 '15 at 9:53