Hi @Donald Symmons,
You can't use Session to achieve this because the Session will be cleared when the application stops.
You can determine if the generated auto-generated reference number already exists in the database. If so, you can re-run the GetAutoNumber() method.
private void AutoGenerateNumber()
{
try
{
string number = GetAutoNumber();
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlDataReader dr;
using (SqlCommand cmd = new SqlCommand())
{
cmd.Parameters.Clear();
cmd.CommandText = "SELECT reference_no FROM IDTable WHERE reference_no = @reference_no"; // Here I tried to check the table to see if the Reference number numbers exist. And if it exists then it should automatically create a new one
cmd.Parameters.AddWithValue("@Reference_no", number);
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
}
if (dr.HasRows)
{
string newnumber = GetAutoNumber();
//Insert a new number
}
else
{
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread