I have a simple methid in which I am getting records from view based on semester_ID like below
public int Get_semesterCode(long semID)
{
var findCode=(from nn in db.SemesterView where vv.semesterId==semID.Tostring() select vv).firstOrDefault();
}
you can see my semID is long and the semesterId coming from view is having varchar(40) as type . I cannot change this long to string as it is requirement that the parameter semID must be long so how to compare , as work around i wrote a simple line like below
string semesid=semID.ToString()
it have value collected and converted to string and semesid get the value but after that when the below code runs it gives exception
var findCode=(from nn in db.SemesterView where vv.semesterId==semID.Tostring() select vv).firstOrDefault();
}
Naimish MakwanaPosted May 13, 2023, 12:17 PM
The exception you are encountering is likely due to a type mismatch between the data types of
semIDandsemesterIdin the database. SincesemIDis alongandsemesterIdis avarchar(40), the comparison between the two will fail because they are not of the same type.To avoid this issue, you can convert the
semesterIdvalue to alongbefore comparing it tosemID. One way to do this is by using thelong.TryParsemethod:Thanks