I have created a daily sales recording system for a grocery shop where until 1000 records it works perfectly. After 1000th record its not auto-incrementing but I can manually change the primary key value and insert the data as required.
This is the code I have used for auto incrementing:
void oil_ID_auto()
{
try
{
SqlCommand selectCommand = new SqlCommand("Select OilId from Oiltbl", conn);
SqlDataReader reader = selectCommand.ExecuteReader();
bool rowFound = reader.HasRows;
string OilId = null;
if (rowFound)
{
while (reader.Read())
OilId = reader[0].ToString();//003
string customerIDString = OilId.Substring(1);
int UserID = Int32.Parse(customerIDString);
int ProductIdInt = 0;
if (UserID >= 0 && UserID < 9)
{
ProductIdInt = UserID + 1;
txtoid.Text = "O00" + ProductIdInt;
}
else if (UserID >= 9 && UserID < 99)
{
ProductIdInt = UserID + 1;
txtoid.Text = "O0" + ProductIdInt;
}
else if (UserID >= 99)
{
ProductIdInt = UserID + 1;
txtoid.Text = "O" + ProductIdInt;
}
}
else
txtoid.Text = "O001";
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error on oil ID generating" + ex.Message, "MAIN Form", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I expect the output as O1001,O1002 etc...but actually it remains O1000 even after O1000 record is inserted