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

    How to create serial number in c# 2010 windows application

    How to generate serial every date starting from "1", but same time i am clicking submit button increasing automatically. (Ex:1,2,3....) and then next day again stating from "1".

    Any one give me some ideas.
  • #766064
    There are multiple ways to implement your logic
    1. you can keep a counter and date in database, on each login check for the date, if it is not current date then reset the counter to one and update current date else you can just increment the counter.
    2. You could use a timer executing every second (for example) and check current date-time.
    see below snippet

    int retval;
    DateTime dt = DateTime.Now;
    if (dt.Hour==0 && dt.Minute==0 && dt.Second==0)
    retval = 0;
    else {
    // Do what you please
    }

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

  • #766072
    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;

  • #766073
    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

  • #766075
    Hi,

    Try below code:

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


Sign In to post your comments