Ms. Corlib Ms. Corlib - 3 months ago 8
SQL Question

Why am I getting "specified cast not valid" here?

I have a column

MyColumn
of type
INT
and I am retrieving some rows with that column

string selectQuery = $@" SELECT [MyColumn] FROM [dbo].[MyTable]
WHERE [Id] IN ('{testid_1}','{testid_2}','{testid_3}')
ORDER BY [MyColumn]";

var vals = mydb.ExecuteQuery<int?>(selectQuery).ToList();


where
db
is of type
DataContext
.

Any idea why this is giving me an invalid cast exception?

Answer

That's because of the below line where it's trying to cast VARCHAR type to INT

WHERE [Id] IN ('{testid_1}','{testid_2}','{testid_3}')
Comments