Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
  if (insuredIdNoArg.HasValue)
{
var x = Convert.ToDecimal(InsuredMasterFields.Id);
                    
if (insuredIdNoArg == Convert.ToDecimal(InsuredMasterFields.Id))
{
bucketPrimary.PredicateExpression.Add(InsuredMasterFields.Id == insuredIdNoArg);
bucketDep.PredicateExpression.Add(InsuredMasterFields.Id == insuredIdNoArg);
}
else
{
bucketPrimary.PredicateExpression.Add(InsuredMasterFields.ExternalId == insuredIdNoArg);
bucketDep.PredicateExpression.Add(InsuredMasterFields.ExternalId == insuredIdNoArg);
}

insuredIdNoArg is the value I'm getting from the text box as input.

InsuredMasterFields.Id is the value I'm getting from the database.

When I try to compare both the values, I get the Invalid cast exception.

Hi,

I was trying to compare the text box value to the database value (Entity framework).
It is throwing invalid cast exception.
Not sure how to handle it.
Your help is highly appreciated. Thanks

What I have tried:

Searched for possible solutions for the exception. Not sure which one to apply.
Posted
Updated 21-Aug-17 7:37am
v4
Comments
Richard MacCutchan 21-Aug-17 10:43am    
The error message is telling you what to do. You cannot use a cast to compare types that are not similar, i.e. do not stem from the same base. That is like saying If (car)pieceofcheese == mycar.
Ananth Asamani 21-Aug-17 10:51am    
Hi Richard,
Thanks for the reply, can you please have a look at the code and help me out.
Karthik_Mahalingam 22-Aug-17 3:21am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900