Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we implement a highest value in textbox which is increment by 1 in page load

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            txt_id= ('SELECT MAX(unitid) + 1  from tblunitmaster');
}
Posted
Updated 18-Sep-17 17:36pm
v5

We cannot do like the above logic. If you write the code in the page load with out any conditions then when ever you post back it will be executed.

Step 1 : In post-back 1st verify if it is a login or a post back.
Step 2 : Then get the value from the Database and set that in a local variable/Session and increase the count in the database.
Step 3 : Separate this logic into a another class or a function to make sure the code is clean and readable.



txt_id= ('SELECT MAX(unitid) + 1 from tblunitmaster');
--> you have to use SQL server connection to connect to the database.

If this dint solve your purpose please reply and explain you req. clearly. Thanks in advance :)
 
Share this answer
 
Comments
Jeyakrishnaraja G 22-Sep-17 3:02am    
Thanks Vaidhya its working for me :)
use ExecuteScalar [^] to read the data from db and display it on any UI control.
have a look on Page.IsPostBack Property [^] since the page_Load event will be fired when there is post back in the page which in turn, will increment the value in the textbox.
 
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