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.

Loading

Categories: C#

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.