Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am creating windows application using c# 2010 windows application, in my application i am using below code for bill number generation, but here i want how to my bill number every day starts from 1. give me any one some ideas.

public void AutoNumber()
{

SqlConnection con = new SqlConnection(db.Connectionstring());
con.Open();

SqlCommand cmd = new SqlCommand("SELECT max(billno) +1 FROM billing", con);

SqlDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{

while (dr.Read())
{
lblbillno.Text = dr[0].ToString();

if (lblbillno.Text == "")
{
lblbillno.Text = "1";
}


}
}

else
{
lblbillno.Text = "1";

}
con.Close();
}


What I have tried:

How to generate bill number every day starts from 1
Posted
Updated 3-Feb-17 0:35am
Comments
Afzaal Ahmad Zeeshan 3-Feb-17 4:35am    
Never mind.

You have to store the date of the last bill number. Then if such a date doesn't match with the current one, restart with billno=1.
 
Share this answer
 
Comments
OriginalGriff 3-Feb-17 4:31am    
Or, you can use PARTITION BY to give an automatic count by the day when you query the DB...
Never ever do that. while it may appear to work fine, in a production environment it is a recipe for disaster as multiple users end up with the same value and your entire database very quickly becomes corrupted beyond belief.

Use an IDENTITY column in your DB, and let SQL sort out unique numbers. You can then use a subquery to return a incremental value per day by using PARTITION BY:
SELECT ROW_NUMBER() OVER (PARTITION BY EnterDate  ORDER BY RowIDValueColumn ASC) AS DailyNumber, * 
FROM MyTable 
WHERE EnterDate >= '2016-02-02' 
ORDER BY RowIDValueColumn ASC
 
Share this answer
 
As OriginalGriff has said, you don't. Let the SQL server manage it.
On the other hand, if your bill number has a certain format besides a running number, you may want to take a look at this Ask Your Database for that Unique ID[^].
 
Share this answer
 

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