Avatar of ksd123
ksd123
 asked on

Issue with type casting in c#

Hi Experts,

I am getting following error when I try to retrieve value (1 or 0) from database.How to get rid of this error?

"operator == cannot be applied to operands of type bool and int"

//employeerepo.cs

pubic bool checkifempexist(int ecode)

{

//if employee exist in database  returns non-zero else returns zero

if(ecode==0)

 return false; //non-zero

else

return true;  //zero

}

Open in new window


Code to check if employee code exist or not

//employee.cs

bool isemployeeexists(employee e)

{

var rep=new employeerepo();

//retrieves 1 or 0 
var empcode=rep.checkifempexist(e.code)

if(empcode==1)  //Throwing error here
{
return true
}
return false;
}

Open in new window

C#.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
louisfr

8/22/2022 - Mon
SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

instead of:

//retrieves 1 or 0
var empcode=rep.checkifempexist(e.code)

if(empcode==1)  //Throwing error here
{
return true
}
return false;


use this:
//retrieves 1 or 0
return (rep.checkifempexist(e.code) > 0);  true if the checkifempexists returns anything greater than zero
ASKER CERTIFIED SOLUTION
Michael Fowler

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
louisfr

In the isemployeeexists method, why not returning directly the result of the checkifempexist method?
bool isemployeeexists(employee e)
{
    var rep=new employeerepo(); 
    return rep.checkifempexist(e.code);
}

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck