Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day.

I have an application that generates student ID based on year, like the first ID:
202100001, then 202100002 and so on. Basically takes the year and start incrementing the counter.
That's good and easy, but my problem because it taking the last ID from database to generate the student ID.
When there is a new year, the year part changes but it doesn't rest to zero.
I would like in 2022 to start at: 202200001 , then 202200002 and so on.
Is there an easier way to reset the counter, should I add a new table for this?

What I have tried:

C#
<pre>//Generate Student Number
        string StudentNumber;
        private void GenerateStudentNumber()
        {
            DateTime moment = DateTime.Now;
            string year = moment.Year.ToString();
            try
            {
                StudentNumber = GenerateID();               
                txtStudentNumber.Text = year + StudentNumber;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private string GenerateID()
        {
            
            string value = "00";
            int IDindex = 0;
            try
            {
                
                using (con = new SqlConnection(databaseConnectionString))
                {
                    // Fetch the latest ID from the database
                    con.Open();
                    cmd = new SqlCommand("SELECT TOP 1 StudentID FROM Students order BY StudentID DESC", con);
                    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        IDindex = Convert.ToInt16(rdr["StudentID"]);
                    }
                    rdr.Close();
                }
                    
                IDindex++;
                // Because incrementing a string with an integer removes 0's
                // we need to replace them. If necessary.
                if (IDindex <= 9)
                {
                    value = "00" + value + IDindex.ToString();
                }
                else if (IDindex <= 99)
                {
                    value = "0" + value + IDindex.ToString();
                }
                else if (IDindex <= 999)
                {
                    //value = "00" + value + IDindex.ToString();
                    value = value + IDindex.ToString();
                }
                else if (IDindex <= 9999)
                {
                    value = "0" + IDindex.ToString();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return value;
        }



Here is how my SQL table is:

SQL
CREATE TABLE [dbo].[Students] (
    [StudentID]          INT             IDENTITY (1, 1) NOT NULL,
    [StudentNumber]      NVARCHAR (50)   NOT NULL,
);


Thanks for your suggestions.
Posted
Updated 16-May-21 17:30pm

1 solution

First of all, I would recommend using a surrogate key, see Surrogate key - Wikipedia[^]. Typically the purpose of the key is to uniquely identify the record, nothing more. Unless there's a good reason to use a key that is derived from the data avoiding it would make life much easier.

Having that said if really you need to use a key like you described, why not split the field into two, store the year separately from the running id. In other words
SQL
CREATE TABLE [dbo].[Students] (
    [StudentIDYear]      INT             NOT NULL,
    [StudentIDNo]        INT             NOT NULL,
    [StudentNumber]      NVARCHAR (50)   NOT NULL
);

This way it would be easy to query the highest StudentIDNo for current year and decide what values you want to use for the new record.

But as said, looking at your table I would prefer the identity field. Also if the StudentNumber is really a number, you should store it in an integer field, not in character field.
 
Share this answer
 
Comments
katela 17-May-21 5:36am    
Thanks for your suggestion. Will think about it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900