Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.'

i want to get the UserId whose otp match with the opt enter by the user.

What I have tried:

public JsonResult VerifyOTP(string otp)
{
using (var db = new OfficeEntities())
{
Account account = new Account();
var num=(int.Parse(otp));

db.Accounts.Where(x => x.Otp == num).Select(x => x.UserId).SingleOrDefault();

if (account == null) {
return Json(false);
}
account.IsActive = true;
db.Entry(account).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return Json(true);

}
Posted
Updated 23-Aug-19 21:13pm
v3
Comments
phil.o 23-Aug-19 7:12am    
The code-block you provided and the error message do not match; in your code, search for a method called ToInt32 accepting a string as argument and returning an integer. When you find it, you can replace it by int.Parse() or int.TryParse() method.

1 solution

That code still makes no sense:
C#
Account account = new Account();
Declare a variable to hold an instance of the Account class, and set it to a new instance of the Account class.
C#
db.Accounts.Where(...).Select(...).SingleOrDefault();
Declare and execute a LINQ query. Throw away the result.
C#
if (account == null) { ... }
You know it's not null. You set it to a new instance of the Account class, and you've never updated it.
C#
account.IsActive = true;
You're updating a blank instance of the class, which is not connected to your database.
C#
db.SaveChanges();
There are no changes to save, because you have not changed any entities connected to your database.

As I mentioned in the comments to your previous question, use:
C#
Account account = db.Accounts.SingleOrDefault(...);

Combining that with Maciej's solution gives:
C#
public JsonResult VerifyOTP(int otp)
{
    using (var db = new OfficeEntities())
    {
        Account account = db.Accounts.SingleOrDefault(x => x.Otp == otp); 
        if (account == null) 
        { 
            return Json(false);
        }
        
        account.IsActive = true;
        db.SaveChanges();
        return Json(true);
    }
}
 
Share this answer
 
Comments
Maciej Los 23-Aug-19 9:38am    
Well explained!
Member 14552976 24-Aug-19 2:49am    
here i want to select the id whose otp is match with the otp enter by the user then update IsActive to true

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