是Employee类型 出现报错信息是 无法将类型为“Employee”的对象强制转换为类型“Employee”。

解决方案 »

  1.   


    我在用ScriptManager的时候也遇到了这样怪异的错误信息?? 哪位能解释下
      

  2.   

    情况是这样的
    Employee.cs   using System;public class Employee
    {
        public Employee() { }    private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }    private string _employeename;
        public string EmployeeName
        {
            get { return _employeename; }
            set { _employeename = value; }
        }    private bool _sex;
        public bool Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }    private string _employeecode;
        public string EmplyeeCode
        {
            get { return _employeecode; }
            set { _employeecode = value; }
        }    private int _department;
        public int Department
        {
            get { return _department; }
            set { _department = value; }
        }    private string _employeepassword;
        public string EmployeePassword
        {
            get { return _employeepassword; }
            set { _employeepassword = value; }
        }    private bool _employeestate;
        public bool EmployeeState
        {
            get { return _employeestate; }
            set { _employeestate = value; }
        }    private string _re;
        public string Re
        {
            get { return _re; }
            set { _re = value; }
        }    private int _turn;
        public int Turn
        {
            get { return _turn; }
            set { _turn = value; }
        }    public Employee(int id, string employeeName, bool sex, string employeeCode, int department, string employeePassword, bool employeeState, string re, int turn)
        {
            this.Id = id;
            this.EmployeeName = employeeName;
            this.Sex = sex;
            this.EmplyeeCode = employeeCode;
            this.Department = department;
            this.EmployeePassword = employeePassword;
            this.EmployeeState = employeeState;
            this.Re = re;
            this.Turn = turn;
        }
    EmployeeBLLusing System;
    using System.Data;
    using System.Collections.Generic;[System.ComponentModel.DataObject]
    public class EmployeeBLL
    {
        public EmployeeBLL() { }    public List<Employee> GetEmployee(bool orderType,int department,int employeeState)
        {
            List<Employee> employees = new List<Employee>();
            string strWhere = string.Empty;
            if (employeeState != -1)
            {
                strWhere = "EmployeeState=" + employeeState.ToString();
            }        if (department != 0)
            {
                if (string.IsNullOrEmpty(strWhere))
                    strWhere = "Department=" + department.ToString();
                else
                    strWhere += " AND Department=" + department.ToString();
            }
            DataSet dataSet = CommonDAL.PageInationDataSet("Employee", "*", "Turn", 0, 0, orderType, strWhere);
            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
            {
                Employee employee = new Employee();
                employee.Id = Convert.ToInt32(dataRow["Id"]);
                employee.EmployeeName = Convert.ToString(dataRow["EmployeeName"]);
                employee.Sex = Convert.ToBoolean(dataRow["Sex"]);
                employee.EmplyeeCode = Convert.ToString(dataRow["EmployeeCode"]);
                employee.Department = Convert.ToInt32(dataRow["Department"]);
                employee.EmployeePassword = Convert.ToString(dataRow["EmployeePassword"]);
                employee.EmployeeState = Convert.ToBoolean(dataRow["EmployeeState"]);
                employee.Re = Convert.ToString(dataRow["Re"]);
                employee.Turn = Convert.ToInt32(dataRow["Turn"]);
                employees.Add(employee);
            }
            return employees;
        }
    }
    Employee.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="XinYun_RadControls.test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"
                OnRowDataBound="GridView1_RowDataBound">        </asp:GridView>
        
        </div>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetEmployee" TypeName="EmployeeBLL">
                    <SelectParameters>
                        <asp:Parameter DefaultValue="True" Name="orderType" Type="Boolean" />
                        <asp:Parameter DefaultValue="0" Name="department" Type="Int32" />
                        <asp:Parameter DefaultValue="-1" Name="employeeState" Type="Int32" />
                    </SelectParameters>
            </asp:ObjectDataSource>
        </form>
    </body>
    </html>
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.DataItem != null)
                    {
                        Employee employee = (Employee)e.Row.DataItem;
                    }
                }
            }
    红色部分执行不过去
      

  3.   

    是Employee类型 出现报错信息是 无法将类型为“Employee”的对象强制转换为类型“Employee”。
    ============
    类型不对.
    错误报的很明显
      

  4.   

    但不用objectdatasource的时候,手动绑定
    EmployeeBLL bll = new EmployeeBLL();
    gridview1.datasource = bll.GetEmployee();
    gridview1.databind();
    这就没问题
      

  5.   

    用三层不建议用objectdatasource,虽然被很多大侠痛批。再申明一下,偶的小小经验是带来的方便远不如带来的问题头疼~。
      

  6.   

    无法将类型为“Employee”的对象强制转换为类型“Employee”?
    难道这两个不是同一个类?建议改另外一个类名试试。
      

  7.   

    Employee employee = (Employee)e.Row.DataItem; 
    =======
    你看一下DataItem是什么.设个断点调试一下.
      

  8.   

    是够头疼的,明明跟踪到这是个Employee类型,就是不能用啊,难道还是要用DataBinder.Eval?
      

  9.   

    我试过了,用response.write(e.Row.DataItem)还是Employee
      

  10.   

    难者不会,会者不难。主要是你不知道如何调试。你至少可以在断点下在即时窗口(或者监视窗口)打印    ?e.Row.DataItem.GetType().FullName 
       ?e.Row.DataItem.GetType().Assembly.FullName 
       ?typeof(Employee).FullName
       ?typeof(Employee).Assembly.FullName嘛!如果要求证你到底有些什么类型,当然要如此打印,不能全凭嘴说。查找bug肯定要动手,而不是仅凭大脑分析。
      

  11.   


    调试,应该使用debug中断。使用那种打印,费时费力而且你做不了多少调试动作。
      

  12.   

    刚转到.net不久,不会调试啊,有谁试过了吗?的确是Employee类型啊
      

  13.   

    dataitem在强制转换类型时出错。你该这么做,把每个row中的值取出来。然后用employee类的new方法去把每个值再放进去。就不会出现问题了。
      

  14.   

    这位大哥能不能解释为什么用手动绑定可以,用ObjectDataSource数据源为什么不行呢?
      

  15.   

    设断点,F10,F11,F5按一变就知道做什么用的了。
      

  16.   

    是否是因为操作的源对象不同呢?页面上直接ObjectDataSourc绑定,是否你绑定的源与构造的类型不一样?~~~没碰过,睡先,明天再来看看
      

  17.   

    e.Row.DataItem 强制转换类型 肯定会出问题的。建议:Employee employee = new Employee()
    employee.Add(e.Row.DataItem);
      

  18.   

    是不是同一个Employee?注意一下命名空间
      

  19.   

    Employee是个实体类,不是IList,
    照你的思路应该是
    List<Employee> employees = new List<Employee>();
    employees.Add(e.Row.DataItem);
    这还是不行,现在用手动绑定没有问题,不能用ObjectDataSource,有点郁闷的说。
      

  20.   

    现在调试了一下,
    e.Row.DataItem.GetType().Assembly.FullName >>> App_Code.rub9ym4b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
    ?typeof(Employee).Assembly.FullName >>> XinYun_RadControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 
    是何原因?
      

  21.   

    我估计在你的整个解决方案里面有两个Employee这样的classsEmployee employee = (Employee)e.Row.DataItem;e.row.dataitem的employee的合名(命名空间+类名)与当前所有的命名空间下面的Employee不相符。建议你把这样写断点到e.row.dateitem的时候,看一下它的类型。
      

  22.   

    我把Employee.cs是放在App_Code\TB\下的
    断点我设了
    ?e.Row.DataItem.GetType().FullName和?typeof(Employee).FullName都是Employee
    e.Row.DataItem.GetType().Assembly.FullName 是 App_Code.rub9ym4b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null 
    ?typeof(Employee).Assembly.FullName 是 XinYun_RadControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    估计就是这原因,就是不知道怎么解决。把Employee.cs放在app_code外?
      

  23.   

    你用项目的类的全名
    比如你的逻辑层的命名空间是:
    更方便的方法是你到逻辑层里面看你的Employee.cs文件,有一行namespace 
    现在假如是namesapce BusuinessBusiness.Employee employee = (Business.Employee)e.Row.DataItem;