Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to cast object of type 'System.String' to type 'System.Int32' while using ToListAsync()

While loading values of a database to a list (using ToListAsync()) I get this error

This is the model I’m using:

[Key]
public int id { get; set; }
public String password { get; set; }

this is the code behind (.cshtml.cs) code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private readonly ApplicationDbContext _db;
public PassStrengthModel(ApplicationDbContext db)
{
    _db = db;
}
public IList<toptenthousand> toptenthousand { get; set; }
public async Task OnGetAsync()
{
    toptenthousand = await _db.toptenthousand.ToListAsync();
}

Database I’m working with screenshot of db screenshot of datatypes | github link (I’ve added an id column which is not present in the repo)

I can’t understand the problem here,
is it problem with db having both string and int values and me keeping the "password" var as string in the Model,
or I’m using ToListAsync() in a wrong way?

>Solution :

I think the "Id" column in the database is nvarchar type or some sort of text type.
The function ToListAsync() tries to convert a string to an int, and the only int in the model is the id. So I guess the id in the database is string type.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading