Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Everyone,
I'm creating a society maintenance desktop application in c# windows form application,
I want to generate the invoice number automatically and in the new year, it should get reset automatically.
Can anybody help me out with this, please?

Or is there any way to get the last entered id in the c# windows form application..?

What I have tried:

public dynamic GetLastInsertId ();
Posted
Updated 13-Jan-20 9:54am
Comments
MadMyche 13-Jan-20 15:39pm    
Are you going to be saving invoice data to a database?
Himanshu Nanikwal 14-Jan-20 12:49pm    
yes I'm storing that data in db
Tomas Takac 14-Jan-20 3:51am    
How about
Guid.NewGuid().ToString("N")
or
DateTime.UtcNow.ToString("yyyyMMddHHmmss")
if you want to generate "unique" id.

1 solution

Use a database to store the invoice details, and set the Invoice number column as an IDENTITY field: that way the DB will automatically assign the next value for you, and you can't change or duplicate it (both of which are important for invoices as the Tax Man will get very, very interested if you do, and your seriously don't want that).

Set up two tables, Invoices and InvoiceLines.
Invoices has the invoice number, date, and customer details.
InvoiceLines has an ID, a FOREIGN KEY column to the invoice number, and the details of one line (or item) on the invoice.

That way, you have one Invoice row, and a bunch of InvoiceLines which refer back to it. From there, you can generate all the info you need, and have flexible invoicing.

Don't try to generate sequential numbers yourself: that only ever works when there is a single user / single instance of the app and that's not likely for an invoicing system. Manual generation causes enormous problems because duplicates will happen and sorting out the resulting mess is an horrible manual task.
 
Share this answer
 
Comments
Himanshu Nanikwal 14-Jan-20 12:48pm    
Thank you so much for guidance!!
Can you tell me one more thing .. how to retrieve lastly entered record from the db in c# windows form application?
OriginalGriff 14-Jan-20 14:00pm    
Depends on what you are trying to do, but you probably don't mean "the last entered record" simply because most times a database is used it is in a multiuser environment - and if you retrieve the actual "last entered" that may have been by a different user!
If you are trying to fetch an IDENTITY value then do your INSERT operation and follow it with a SELECT SCOPE_IDENTITY() on the same connection object and it will return the last one created. (There are other ways to get IDENTITY values but that's probably the one you want most of the time.)
Himanshu Nanikwal 15-Jan-20 14:49pm    
string sql = "INSERT INTO tbl_Maintenance(Name, PhoneNo ,HouseType, Block, HouseNo, MDate, MCharges, PCharges, Total, PaymentMode) " +
"VALUES (@Name, @PhoneNo, @HouseType, @Block, @HouseNo, @MDate, @MCharges, @PCharges, @Total, @PaymentMode) " +
"Select @@IDENTITY as identityvalue ";

I wrote this code on button, now how to store the identityvalue in the local variable ..?

I tried this way..
int invoice ;

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
invoice = reader.GetInt32(0);

MessageBox.Show(invoice.ToString());
}

reader.Close();

but I got the error "Specified cast is not valid"

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