Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
How to generate serial number every day staring from "1" in c# 2010 windows application


What I have tried:

How to generate serial number every day staring from “1” in c# 2010 windows application
Posted
Updated 15-May-16 4:35am
Comments
RickZeeland 16-May-16 4:23am    
For a login form solution, take a look here: http://www.codeproject.com/Articles/106751/Splash-and-Login-Forms-Problems-and-Solutions

windows application - do you mean WinForms ? or Console ?? either way, you dont say when the serial number 'wraps around' - you could just use the something like

C#
StringBuilder sb = new StringBuilder(DateTime.Now.Format("yyyy");
sb.append("{0:D3}", DateTime.Now.DayOfYear);
string serial = sb.ToString();


that would give you 2016001..2016366 (2016 is/was a leap year)
 
Share this answer
 
Comments
Boopalslm 15-May-16 8:27am    
where i am write this code, and then number shown on text box where to write above code
Garth J Lancaster 15-May-16 8:30am    
oh, so its a WinForms project - thank you so much for including that in your original question

I'd add the code to the Form_Load() event - since you havnt told us anything useful, like the name of your textbox, I'll assume its textbox1, in which case, after writing the code, you would write

textbox1.Text = serial;
Boopalslm 15-May-16 8:35am    
yes this win forms, but i am write your code form page load event but error is came, first and second line.
Garth J Lancaster 15-May-16 8:40am    
Im pretty sure you can figure out that the first line is missing a ')' and since you dont post any useful information, who knows about the 2nd line

Please learn how to use a debugger & google to look up simple issues
Boopalslm 15-May-16 8:44am    
fist line error in Foramt, second line eror in append
A simple solution to get you started:
C#
private static int GetSerialNumber()
{
    string start = "2016-05-14";
    return (DateTime.Now - DateTime.Parse(start)).Days;
}
 
Share this answer
 
v2
Comments
Boopalslm 15-May-16 8:27am    
where i am write this code, and then number shown on text box where to write above code
RickZeeland 15-May-16 8:41am    
Where ever you want, e.g. in form initialize:
myTextBox.Text = GetSerialNumber().ToString();
Boopalslm 15-May-16 8:49am    
yes its working fine thanks, but how to increment serial number every time submit the form vales and then next day when a login again serial number starting from 1
RickZeeland 15-May-16 8:59am    
Ah, you don't need the GetSerialNumber() function at all.
I will add a new solution, just wait a few minutes !
Boopalslm 15-May-16 9:01am    
k thanks i am waiting
You need to keep a reference date that you use to compare with the current date when you create the serial number.
If the current date is different from the reference date, you reset the serial number to 1.
You also need to store the last serial number, so you can close the application and continue with the next number in the sequence when you start it again.

You can use the Application Settings for this, see Application Settings[^]

You can also use other methods to store the variables, e.g. a text file or XML file.

When creating a new serial number:
C#
if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date)
{
    Properties.Settings.Default.ReferenceDate = DateTime.Now;
    Properties.Settings.Default.LastSerialNumber = 1;
}
else
{
    Properties.Settings.Default.LastSerialNumber++;
}


When the program closes:
C#
Properties.Settings.Default.Save();


Cannot swear that the syntax is 100% correct, but the principal should be right.

[UPDATE]
This code should do the job.
C#
using System;
using System.Windows.Forms;

namespace SerialNumberReset
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tbSerialNumber.Text = GetSerialNumber().ToString();
        }

        private int GetSerialNumber()
        {
            if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date)
            {
                Properties.Settings.Default.ReferenceDate = DateTime.Now;
                return 1;
            }
            else
            {
                return Properties.Settings.Default.SerialNumber;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void buttonUpdateSerialNumber_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.SerialNumber = GetSerialNumber() + 1;
            tbSerialNumber.Text = Properties.Settings.Default.SerialNumber.ToString();
        }
    }
}
 
Share this answer
 
v3
Another, even simpler solution !
C#
using System;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form7 : Form
    {
        private int serialnumber = GetSerialNumber();

        public Form7()
        {
            InitializeComponent();
        }

        // Submit button.
        private void button1_Click(object sender, System.EventArgs e)
        {
            this.textBox1.Text = serialnumber.ToString();
            serialnumber++;
        }

        private static int GetSerialNumber()
        {
            // Get the last serialnumber from settings.
        }
    }
}
 
Share this answer
 
v3
Comments
Boopalslm 15-May-16 9:10am    
k it's working fine thanks a lot, but next day morning it's again starting from 1
George Jonsson 15-May-16 9:12am    
This will only work if the application is started once a day, and the computer is never restarted during the day.
Boopalslm 15-May-16 9:16am    
k but i am restarted my computer how to get continues serial number
RickZeeland 15-May-16 9:25am    
Confusing, confusing, but I updated the solution.
RickZeeland 15-May-16 9:28am    
I think you need something more like Solution 3, which saves the serialnumber.
Another option might be to edit \Properties\AssemblyInfo.cs as follows:
C#
[assembly: AssemblyVersion("1.0.*")]
//  [assembly: AssemblyVersion("1.0.0.0")]
//  [assembly: AssemblyFileVersion("1.0.0.0")]

and then get the revision number as follows:
C#
private static int GetRevisionNumber()
{
    var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    return assemblyVersion.Revision;
}

In theory this should increment the build number every time you build, but I find it's behaviour is a bit quirky.
 
Share this answer
 
Comments
George Jonsson 15-May-16 11:21am    
A bit of a hack and it doesn't solve the problem with the date.
Boopalslm 16-May-16 4:01am    
Another one doubt – I am creating login form (enter username (Text Box), password (Text Box) & rights(Using combo box) ,I am select a rights from user
That time my forms button disable, after that I again goes to login enter rights form Admin that time button visible, how to create in c# windows application 2010,
I am using MDI Parent form, without MDI parent working nice, but I am using MDI Parent forms not working (it means button visible both login), how to show my button control only admin login.
George Jonsson 16-May-16 4:06am    
If you have doubts, you should probably go to church.
If you have another question, please post a new question so you can get a proper answer.
The comments are not really made for posting code and ask new questions.
Boopalslm 16-May-16 4:05am    
this is my login form code

private void button1_Click(object sender, EventArgs e)
{


SqlConnection con = new SqlConnection(db.Connectionstring());
con.Open();
SqlCommand cmd = new SqlCommand("select * from usercreation where username='" + txtuser.Text + "' and password='" + txtpassword.Text + "' and rights='" + comboBox1.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();


if (dr.HasRows)
{

while (dr.Read())
{
string log = dr[3].ToString();



if (log == "User")
{


adminmdipanel adpanel = new adminmdipanel();
adpanel.Show();

usercreation uc = new usercreation();
uc.find.Visible = false;

uc.Show();



this.Hide();





}



else if (log == "Admin")
{


adminmdipanel adpanel = new adminmdipanel();
adpanel.Show();


usercreation uc = new usercreation();
uc.find.Visible = true;


uc.Show();


this.Hide();


this.Hide();
}

else
{
MessageBox.Show("Try Again");
}


}




}


dr.Close();
con.Close();
}
George Jonsson 16-May-16 4:07am    
As I said, please don't post code in the comment section.
Post a new question.

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