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 :)