Max id is working fine until the id Q/21/9 but as soon as the id increments to Q/21/10. SQL Server keeps returning Q/21/9 as max id.
See in image. Max id Q/21/10 exists.
But SQL Server returns Q/21/9 whereas Q/21/10 is expected.
Below is my code that i am using to generate custom auto incremental id.
private static int qid = 1;
public string GetQuotationId()
{
Connection con = new Connection();
if (ConnectionState.Closed == con.connect.State)
{
con.connect.Open();
}
string query = "SELECT MAX(QuotationReference) FROM Table_Inquiry";
SqlCommand cmd = new SqlCommand(query, con.connect);
try
{
var maxid = cmd.ExecuteScalar() as string;
if (maxid == null)
{
return "Q/" + (DateTime.Today.Year % 100).ToString() + "/" + qid.ToString() + "";
}
else
{
int intval = int.Parse(maxid.Substring(5));
intval++;
return String.Format("Q/" + (DateTime.Today.Year % 100).ToString() + "/{0:}",intval);
}
}
catch
{
throw;
}
}