chirag
chirag

Reputation: 57

How to create Invoice Number in Text box and auto increment it on button click?

I am using Asp.net(C#), and SQL Server. In web application I want 5 digit Invoice number in text box, and after clicking on button the gridviews data to be saved in database, and then invoice number in the text box should be increment by one...and so on. I used this code =>

protected void Page_Load(object sender, EventArgs e)
{
    double ID = 00001;
    txtinvoiceno.Text = Convert.ToString(ID);

and

protected void btnsavef1_Click(object sender, EventArgs e)
{
    ID++;
    var no = Convert.ToInt32(ID);
    txtinvoiceno.Text = Convert.ToString(no);
}

but it shows in the output only single digit 1, and increment it only once time i.e.2, and further increment is not working.

Anyone have any idea on this??

Thanks & Regards,

Upvotes: 5

Views: 10040

Answers (5)

Beldi Anouar
Beldi Anouar

Reputation: 2180

Try this :

public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           if (Page.IsPostBack == false)
           { 
               int ID = 1; 
               txtinvoiceno.Text = ID.ToString("D5");
             }
        }

        protected void btnsavef1_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt16(txtinvoiceno.Text );
            ID++;
            txtinvoiceno.Text = ID.ToString("D5");
        }

    }

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You´re looking for formatting, which is very easy in your case:

   //DONE: int, not double - do you want to deal with round-up errors?
   //DONE: ID should be a field, not a local variable
   private int ID = 0;

   protected void Page_Load(object sender, EventArgs e)
   {
       // whenever you want 5 digits - just put 5 zeroes when formatting
       txtinvoiceno.Text = ID.ToString("00000");
       ...
   } 

   protected void btnsavef1_Click(object sender, EventArgs e)
   {
       ID += 1;
       // once again: want 5 digits - put 5 zeroes when formatting
       txtinvoiceno.Text = ID.ToString("00000");
   }

A better choice, IMHO, is to convert ID into property and hide formatting there:

   private int m_ID;

   public int ID {
     get {
       return m_ID;
     }
     set {
       m_ID = value;
       txtinvoiceno.Text = m_ID.ToString("00000"); 
     }
   }      
   ...
   protected void Page_Load(object sender, EventArgs e) {
     // Just assign
     ID = 0;
     ... 
   }

   protected void btnsavef1_Click(object sender, EventArgs e) {
     // Just increment 
     ID += 1;
   } 

Upvotes: 1

Meer
Meer

Reputation: 2835

you need to format only result number after increment.

protected void btnsavef1_Click(object sender, EventArgs e)
{
  ID++;
  var no = Convert.ToInt32(ID);
  txtinvoiceno.Text = no.ToString().PadLeft(5, '0');
}

if ID=7 your result will be after Increment will be 00008

Upvotes: 0

Beldi Anouar
Beldi Anouar

Reputation: 2180

Try this :

    public partial class Form2 : Form
    {
        private double ID=0;
        public Form2()
        {
              InitializeComponent();
        }
        protected void Page_Load(object sender, EventArgs e)
         {
         double ID = 00001;
         txtinvoiceno.Text = Strings.Format(ID, "000000")
        }

   protected void btnsavef1_Click(object sender, EventArgs e)
   {
     ID++;
     var no = Convert.ToInt32(ID);
      txtinvoiceno.Text = Strings.Format(no, "000000")
    }
}

Upvotes: 0

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

I doubt this will even compile, as within btnsavef1_Click the variable ID is not defined. You probably want ID to be an instance-variable:

class MyClass {
    private int ID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtinvoiceno.Text = this.ID.ToString("D5");
    }

    protected void btnsavef1_Click(object sender, EventArgs e)
    {
        this.ID++;
        txtinvoiceno.Text = this.ID.ToString("D5");
    }
}

Furthermore you won´t actually store the formatted number. Simply store the number as integer and do the formatting within your events-code using ToString and format-specifiers.

Upvotes: 5

Related Questions