Ramco Ramco

Ramco Ramco

  • 443
  • 3.4k
  • 527k

Error-Specified cast is not valid

Jan 24 2022 11:06 AM

Hi

  I am getting above error . Data file also attached.

public List<Employee> GetAllEmployee()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn0"].ConnectionString);
            string query = "select empid,firstname,lastname,dept,sex from Oh ";
            SqlCommand cmd = new SqlCommand(query, conn);
            List<Employee> Employee = new List<Employee>();
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Employee.Add(new Employee
                    {
                        EmpId = (int)dr[0],
                        FirstName = dr[1].ToString(),
                        LastName = dr[2].ToString(),
                        Dept = (int)dr[3],
                        Sex = dr[4].ToString()
                    });
                }
            }
            conn.Close();
            return Employee;
        }

Thanks


Attachment: Data.rar

Answers (7)