Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Use This Code For Auto number in Text Box
But After When I Saved 10 Data in my Database this code stop working and this code Show every time number "10" in Text box Not 11,12,13....

So Please Help me Resolve this Problem

Thank you

What I have tried:

C#
public void autonumber()
        {
             String connstring = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
            
            using (OleDbConnection con = new OleDbConnection(connstring))
            {

                using (OleDbCommand cmd = new OleDbCommand("select max (LoanNumber)+1 from customer", con))
                {
                    con.Open();
                    OleDbDataReader dr = cmd.ExecuteReader();

                    if(dr.HasRows)
                    {
                        while(dr.Read())
                        {
                            textBoxLoanNumber.Text = dr[0].ToString();
                            if(textBoxLoanNumber.Text == "")
                            {
                                textBoxLoanNumber.Text = "1";
                            }
                        }
                    }
                    else
                    {
                        textBoxLoanNumber.Text = "1";
                    }
                    con.Close();
                }
            }
        }
Posted
Updated 1-Mar-20 16:08pm
v3
Comments
PIEBALDconsult 1-Mar-20 22:10pm    
Is this only for display? Or do you want to store the numbers? I don't recommend storing the numbers as it tends to lead only to pain down the road.
Of course, the use of MS Access leads only to pain anyway, so maybe you're a glutton for pain?
Richard Deeming 3-Mar-20 9:34am    
My guess is that your LoanNumber column is storing strings rather than numbers. The string "9" is greater than the string "10", because strings are compared character by character, and "9" is greater than "1". So your Max(LoanNumber) will always return "9".

But pay attention to the discussion in solution 1 - this is definitely not the right thing to do.

Auto-numbering is typically something which has to be done on the server side, not on the client side. Because with client-side method, if there are several clients, nothing insures that two clients do not get the same id at the same time.
Performing auto-numbering on client side is an anti-pattern which you should learn avoiding.

sql auto generated id[^]
 
Share this answer
 
v2
Comments
Amar chand123 1-Mar-20 7:19am    
then what is Solution for this Problem
because if i use ms access autonumber there is problem if i delete any data then autonumber not reset and access autonumber option is read-only
phil.o 1-Mar-20 7:31am    
If you delete any data, you should not worry about having a missing value in the sequence. It has no impact at all on the performance, nor on data validity. This is a non-problem.
Amar chand123 1-Mar-20 8:47am    
Is There such code, that can count rows in my database's customer Table
than we add +1 and show in Textbox than i saved database
because now i try save manually data in access autonumber column by c# Textbox and Data Successfully Saved
phil.o 1-Mar-20 9:02am    
As I explained, this is not a correct way to do it like this. Please use auto-incremented primary key on the database, and stop relying on table count to get next id.
Amar chand123 1-Mar-20 9:30am    
i Find a Method And its Working
my Access Database Column is Autonumber But Its Edit by this code
Please Check this Code

private void Autonumber1()
{
String connstring = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
OleDbConnection con = new OleDbConnection(connstring);
string Query = "select count(*) from customer";
OleDbCommand cd = new OleDbCommand(Query, con);
try
{
con.Open();
int count = Convert.ToInt16(cd.ExecuteScalar()) + 1;
textBoxLoanNumber.Text = count.ToString("00000");
textBoxLoanNumber.Enabled = false;
}

finally
{
con.Close();
}
}
If you want to simulate row Numbers in SQL Server you can generate the row number in a query.

that way you can still have an identity column and still return numbered records.
What ever you do DON'T try to use Row Number to link queries.


https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver15

sample:
SQL
USE AdventureWorks2012;   
GO  
SELECT ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS Row,   
    FirstName, LastName, ROUND(SalesYTD,2,1) AS "Sales YTD"   
FROM Sales.vSalesPerson  
WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0;


It will return :
Row FirstName    LastName               SalesYTD  
--- -----------  ---------------------- -----------------  
1   Linda        Mitchell               4251368.54  
2   Jae          Pak                    4116871.22  
3   Michael      Blythe                 3763178.17  
4   Jillian      Carson                 3189418.36  
5   Ranjit       Varkey Chudukatil      3121616.32  
6   José         Saraiva                2604540.71  
7   Shu          Ito                    2458535.61  
8   Tsvi         Reiter                 2315185.61  
9   Rachel       Valdez                 1827066.71  
10  Tete         Mensa-Annan            1576562.19  
11  David        Campbell               1573012.93  
12  Garrett      Vargas                 1453719.46  
13  Lynn         Tsoflias               1421810.92  
14  Pamela       Ansman-Wolfe           1352577.13
 
Share this answer
 
v2

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