是Employee类型 出现报错信息是 无法将类型为“Employee”的对象强制转换为类型“Employee”。
解决方案 »
- 紧急求助:关于使用basepage的问题,请同仁们帮忙看一下,谢谢
- Asp.net text文本框只能输入数字和字母,号码纸能输入11位手机号!
- 网站发送邮件
- Repeater <SeparatorTemplate>模板与表格配合使用的问题
- !!菜鸟问:.aspx格式网页保存成.html格式
- 外面盗版的vs2005是几张盘的?
- 修改程序
- .NET提醒代码
- 搞不懂了,再看别人写的。net mvc DDD 程序,求教
- 相信每位都会遇到这样的问题。。各位帮帮忙
- fileupload控件在修改的时候路径是否可以显示
- 将1篇文章插入到数据库后如何让他在界面上显示出原有格式(我用的时freetextbox添加)C#
我在用ScriptManager的时候也遇到了这样怪异的错误信息?? 哪位能解释下
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;
}
}
}
红色部分执行不过去
============
类型不对.
错误报的很明显
EmployeeBLL bll = new EmployeeBLL();
gridview1.datasource = bll.GetEmployee();
gridview1.databind();
这就没问题
难道这两个不是同一个类?建议改另外一个类名试试。
=======
你看一下DataItem是什么.设个断点调试一下.
?e.Row.DataItem.GetType().Assembly.FullName
?typeof(Employee).FullName
?typeof(Employee).Assembly.FullName嘛!如果要求证你到底有些什么类型,当然要如此打印,不能全凭嘴说。查找bug肯定要动手,而不是仅凭大脑分析。
调试,应该使用debug中断。使用那种打印,费时费力而且你做不了多少调试动作。
employee.Add(e.Row.DataItem);
照你的思路应该是
List<Employee> employees = new List<Employee>();
employees.Add(e.Row.DataItem);
这还是不行,现在用手动绑定没有问题,不能用ObjectDataSource,有点郁闷的说。
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
是何原因?
断点我设了
?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外?
比如你的逻辑层的命名空间是:
更方便的方法是你到逻辑层里面看你的Employee.cs文件,有一行namespace
现在假如是namesapce BusuinessBusiness.Employee employee = (Business.Employee)e.Row.DataItem;