Ms. Corlib
Ms. Corlib

Reputation: 5213

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?

Upvotes: 0

Views: 300

Answers (1)

Rahul
Rahul

Reputation: 77846

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}')

Upvotes: 6

Related Questions