- Summarize the problem
Adding an object to a list after reading its properties from a SQL Server SELECT
statement - I get this error:
System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'
Include details about your goal
- My goal is to read asset changes from the database by using a
SELECT
T-SQL statement, reading and appending them into a list and display them using the list in a view.
Describe expected and actual results
Expected: No error pops up.
Actual: The error mentioned earlier pops up.
Include any error messages
- Already mentioned earlier.
- Describe what you’ve tried
Show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. You can get better answers when you provide research.
Getting the value as an integer, then converting it to a string.
//UserID = reader.GetString(0),
//AssetID = reader.GetString(1),
UserID = reader.GetInt32(0).ToString(),
AssetID = reader.GetInt32(1).ToString(),
- Making sure both variables
UserID
andAssetID
useint
The same error still pops up.
- Show some code
When appropriate, share the minimum amount of code others need to reproduce your problem (also called a minimum, reproducible example)
I don't know how to produce a minimum example without breaking the project in one way or another.
There's the link to download the project. After downloading it, extract and run it.
The email address and password are john@gmail.com
and john123
respectively. You should see "Asset History" on the top navigation bar. Click on it to produce the problem.