Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have controller name emp have action add
i need when action add view loaded display last value increased by one in employeeid textbox
meaning suppose i have in datbase employeeid 1 then when action add view loaded employeeid must have 2
so that how to do that please ?
in action what i write
[httpget]
 public IActionResult Create()
        {
what i write here
}
on view add 
what i write

What I have tried:

<pre>  var results = _context.GetAll().Where(Employee => Employee.EmployeeId> Employee.EmployeeId).OrderBy(Employee => Employee.EmployeeId).FirstOrDefault();
Posted
Updated 6-Jan-19 20:21pm
Comments
Bryian Tan 6-Jan-19 23:36pm    
Just curious, employeeId not auto increment in the table?

If the idea is to define a value for a key for a new record in the database, normally the application does not do this. Instead the database assigns the key when a record is added.

For example if two users are adding a row at the same time, based on your code they could try to use the same ID value for an employee. This would result into key violation or to a duplicate record, depending on your schema definitions.

If this is a SQL Server application, you could utilize IDENTITY (Property) (Transact-SQL) - SQL Server | Microsoft Docs[^] for the Id column to define it as auto-incremented. After the insert, you can get the inserted calue using @@IDENTITY (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Quote:
How to load value of last employeeid + 1 in add action

The whole idea is wrong because an SQL server is multi-user, and if 2 users are adding a new employee at same time, both are ending with same new id.
The only solution is to have the server generate the new id.
this specific request is done by make the id 'auto increment'
SQL AUTO INCREMENT a Field[^]
 
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