Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, i'm newbie in c#. I need help for generate unique number for invoice.
I want to display the number like "SSL/06/XI/2018" but the type value in db still in number.

What I have tried:

void NewInvoice()
        {
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter("Select [Invoice] from [Purchase] order by [Invoice] desc", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                //Row value +1 for increment
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Invoice_tx.Text = (int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1).ToString();
                }
                else
                { Invoice_tx.Text = "1"; }
            }
            catch (Exception x)
            {
                MetroFramework.MetroMessageBox.Show(this, "Error" + x, "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }



I try for display just number(like "1","2") it works. When I try create like "SSL/06/XI/2019" still error. Please Help me
Posted
Updated 21-Jan-19 22:24pm

1 solution

1) Debug so you understand where the error is. It can help to split long lines into multiple lines so a single step in the debugger only does as little as possible. I know debugging might seem like an advanced topic you can learn later. It is not. It is the first skill you need to learn. It makes things simpler, not harder.

2) Error messages try to tell you what is wrong. Read them and take time to understand them. Google the error text will often give hints. And if you still can't find out, at least include the error message when you ask for help.

3) What do you expect
int.Parse("SSL/06/XI/2019")
to return? You first need to read the string, then split it up in parts, then increment the part you want, then reassemble the string. Look into the Split() method on strings. There are other (arguable better) ways to do it, but Split() is very simple conceptually.

This also gives you a good place to detect if the previous invoice was from last year so you need to reset the count.

An alternative approach is to store each part of your invoice number in it's own column in the database and assemble the string as it needs to be printed or displayed.
 
Share this answer
 
Comments
Member 13921023 23-Jan-19 11:37am    
thank you sir, that's help me a lot

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