Click here to Skip to main content
14,218,748 members
Rate this:
Please Sign up or sign in to vote.
See more:
  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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100