Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[HttpGet]
public ActionResult EmployeeView(int id)
{
Employee employee = new Employee();
using (HRMSEntities employeeContext = new HRMSEntities())
{
employee = employeeContext.Employee.Where(x => x.Person_Id == id).FirstOrDefault();
}
return View(employee);
}
}

What I have tried:

[HttpGet]
       public ActionResult EmployeeView(int id)
       {
           Employee employee = new Employee();
           using (HRMSEntities employeeContext = new HRMSEntities())
           {
               employee = employeeContext.Employee.Where(x => x.Person_Id == id).FirstOrDefault();
           }
           return View(employee);
       }
   }
Posted
Updated 1-Mar-18 2:01am
Comments
CHill60 1-Mar-18 7:37am    
You can't compare strings with ints like that ... Person_Id must be a string. Try comparing it to id.ToString() instead
Member 13667386 1-Mar-18 7:44am    
please help me, im new to c#
CHill60 1-Mar-18 8:56am    
You will get quicker responses if you use the "Reply" link for a comment rather than chancing me coming back to a post I've already commented on
Member 13667386 1-Mar-18 7:48am    
how i can fix?
Likefire 1-Mar-18 8:31am    
employee = employeeContext.Employee.Where(x => x.Person_Id.Equals(id.ToString())).FirstOrDefault();

1 solution

You need to change Person_Id from a string to an int - and probably correct it in the source as well. If you are retrieving the values from a DB, and the column datatype is VARCHAR or NVARCHAR, then you need to change that to an integer, if your ID's are supposed to be numeric. Always use the most appropriate datatype; anything else is a PITA.

If you don't then not only do problems like this occur, but you get worse problems when you try to use any form of sorting: strings are sorted according to the first different character, so they do not work "correctly" when sorting numeric values:
1
10
11
...
19
2
20
21
...
 
Share this answer
 
Comments
phil.o 1-Mar-18 8:46am    
5'd, but have a look at previous question; pretty confusing, huh?
OriginalGriff 1-Mar-18 8:50am    
He's currently running his fourth copy of the same question - I've deleted one as a repost already.

Many more, and I'm tempted to start treating this as abuse... :laugh:
OriginalGriff 1-Mar-18 8:51am    
And it looks like Chill's answer has been deleted along with the fourth one ... I got the third ...
phil.o 1-Mar-18 8:53am    
"Put your mouse on the floor! Now! Put your mouse on the floor!"

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