You must Sign In to post a response.
  • Category: .NET

    How to generate serial number every day staring from “1” in c# 2010 windows application

    How to generate serial number every day staring from "1" in c# 2010 windows application
  • #765926
    One way is, You could use a timer executing every second (for example) and check current date-time

    int retval;
    DateTime dt = DateTime.Now;
    if (dt.Hour==0 && dt.Minute==0 && dt.Second==0)
    retval = 0;
    else {
    // Do what you please
    }
    //or
    double retval = DateTime.Now.TimeOfDay.TotalSeconds;
    //OR
    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.
    }
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765929
    Hi Boopal.,

    Try this!

    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();
    }
    }
    }

  • #765939
    Hi,
    You can try this:
    Add key inside app.config file of your windows application as follows:
    <configuration>
    <appSettings>
    <add key="Date" value="15-05-2016"/>
    </appSettings>
    </configuration>

    .CS code:
    Using System.Configuration;
    string szOldDate= ConfigurationManager.AppSettings["Date"].ToString();
    Compare Datetime.Now and szOldDate
    If Datetime.Now > szOldDate then reset serial_No = 1;
    Else serial_No = serial_No + 1;

  • #765955
    How you plan to store the Serial number?. It is just display only.
    If you are plan to store the Serial number in SQL Server/Simple Text File you have have the table having the columns "Date" and Serail number.

    You can increase the serial number by checking the current date. Following the the logic you can use.
    1. Select DBdate,Serialnumber from TableName where DBdate = Current date
    2. If you find the record in the above query, you can increase the current serial number and use it.
    3. If you did not find record in the above query, Insert one record with current date and serial number = 1
    By using the above logic you can reset the serial number based on current date

    By Nathan
    Direction is important than speed


Sign In to post your comments