Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this is my code for auto generated id i want my auto id to get the datetime and year for my id. it will generate like 050-2018-0001 in the textbox like this format im new in C#. sorry for the bad english
C#
private void GenerateID()
        {
           
            con.Open();
            cmd = new MySqlCommand("Select Max(StudID)+1 from student_registration", con);
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {

                while (dr.Read())
                {
                    txtStudID.Text = dr[0].ToString();
                    if (txtStudID.Text == "")
                    {
                        txtStudID.Text = "1";

                    }
                }
            }
            else
            {
                txtStudID.Text = "1";
                return;
            }
            con.Close();
        }


What I have tried:

i need auto generated id in C#
Posted
Updated 5-Aug-18 21:53pm
v3
Comments
OriginalGriff 3-May-18 3:33am    
And?
What have you tried?
Where are you stuck?
What help do you need?
What does "DateTime" have to do with that ID?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

Your question is not very clear, but maybe you mean something like this:
txtStudID.Text = "050-" + DateTime.Now.Year + "-" + dr[0].ToString().PadLeft(4, '0');
 
Share this answer
 
v2
Comments
Member 13809257 3-May-18 4:48am    
when i run the code and saved it. result is 050-2018-0050 not 050-2018-0001
You need to parse you StudId because is it string

try like this

private void GenerateID()
{
	
	con.Open();
	cmd = new MySqlCommand("Select Max(StudID) from student_registration", con);
	dr = cmd.ExecuteReader();            
	string newId = string.Format("050-{0}-0001", DateTime.Now.Year);
	if (dr.HasRows)
	{
		string prefix = string.Format("050-{0}", DateTime.Now.Year);
		while (dr.Read())
		{
			
			string maxId= dr[0].ToString();
			if (!string.IsNullOrWhiteSpace(maxId) && maxId.StartsWith(prefix))                  
			{                        
				int count = Convert.ToInt32(maxId.Split('-')[2]);
				newId = string.Format("050-{0}-{1:0000}", DateTime.Now.Year, count+1);
			}                    
		}
	}
	txtStudID.Text = newId;
	con.Close();
}
 
Share this answer
 
v3
Comments
nurjimar adam 16-Jun-18 8:25am    
sir jekin77 i wanted auto geneated id like 000000000001 000000000002 000000000003 i'm using auto increment.
jekin77 16-Jun-18 8:38am    
It's already autogenarated .. try to understand the code-snippet.
at least run the code and debug it to see the output
nurjimar adam 16-Jun-18 8:49am    
yah but i need like 00000000001 0000000002 generated id can u help me with it
jekin77 16-Jun-18 8:53am    
you wrote in the fist post "i want my auto id to get the datetime and year for my id. it will generate like 050-2018-0001" , so the examle from do that.

Now you will somthing else ...

If you undestand the code below , you can do it yourself ...JUST TRY IT !
nurjimar adam 16-Jun-18 8:59am    
i'm new in programming
The best of way of having Auto Generated Id in C# code is below.

public class Employee
{
public Guid Id { get; private set; } = Guid.NewGuid();
public string EmployeeName { get; set; }
public string Address { get; set; }
}

So whenever you create an object of Employee class it will assign an Unique GUID to ID property so no need to do anything else.
 
Share this answer
 

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