Example : How to get an Automatic Increment of a Serial Number in C#?
private void AutoSlno()
{
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "SELECT ISNULL(MAX(Slno), 0) + 1 FROM UserRegistration";
SqlCommand cmd = new SqlCommand(sql, con);
object result = cmd.ExecuteScalar();
TxtSlno.Text = result.ToString();
}
catch (Exception ex)
{
MessageBox.Show(
"Error: " + ex.Message,
"Database Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
NB : Call this AutoSlno() function on Form1_Load event, Save Code etc.
![]()
0 Comments