Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting exception
Object of type System.Int32 cannot be converted to type System.Boolean

Below is the method...

C#
public Model.PayrollEmpSalarySetup Get(string Record_Id)
        {
            Model.PayrollEmpSalarySetup iObject = null;
            DBManager dbManager = null;
            IDataReader iReader = null;
            string sql = string.Empty;

            try
            {
                sql = @"SELECT SALARY_SETUP_ID, EMP_NO, CREATE_DATE, FROM_DATE, TO_DATE, GROSS_SALARY, BASIC_SAL, HRA_SAL, MED_ALL_SAL, CONVEYANCE_SAL, 
                    OTHER_ALL_SAL, PF_SAL, PT_SAL, NET_SALARY, IS_ACTIVE, MARK_DELETED, LOGIN_ID, SYSTEM_IP,TRANS_TIME
                    FROM dbo.PAYROLL_EMPLOYEE_SALARY_SETUP WITH(NOLOCK) WHERE MARK_DELETED=0 AND EMP_NO=" + Record_Id;

                switch (Constants.DBEngine)
                {
                    case (int)DataProvider.SqlServer:
                        dbManager = new DBManager();
                        dbManager.ConnectionString = Constants.ConnectionString;
                        dbManager.ProviderType = DataProvider.SqlServer;
                        break;
                }
                dbManager.Open();
                dbManager.Command.CommandTimeout = Constants.CommandTimeOut;
                dbManager.Command.CommandText = sql;
                dbManager.Command.Connection = dbManager.Connection;
                iReader = dbManager.Command.ExecuteReader();
                iObject = iReader.SingleOrDefault<Model.PayrollEmpSalarySetup>();
            }
            catch (Exception Exp)
            {
                throw Exp;
            }
            finally
            {
                if (iReader != null)
                    iReader.Close();
                if (dbManager != null)
                    dbManager.Close();
            }
            return iObject;
        }
Posted
Updated 1-Feb-16 4:02am
v2
Comments
CHill60 1-Feb-16 10:05am    
On which line is the exception thrown?
Meer Wajeed Ali 2-Feb-16 3:58am    
iObject = iReader.SingleOrDefault<model.payrollempsalarysetup>();
Richard MacCutchan 1-Feb-16 10:14am    
Do not use string concatenation in SQL, it leaves your database open to SQL injection; use proper parameterised queries.

1 solution

A DataReader doesn't have a method SingleOrDefault<T>(). So it must be an extension method. Which apparently maps the row values to an object of Model.PayrollEmpSalarySetup in this case. And one of these mappings doesn't work because the source column type is of Int32 and the target member type is of type Boolean. You have to adjust either your model or the database column.
 
Share this answer
 

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