无法从'System.Data.DataSet'转换为'Employee'

时间:2015-11-02 15:55:01

标签: c# asp.net

我正在尝试按ID搜索员工记录。所以首先我搜索数据库,然后运行另一个函数来获取图像,但是我收到了这个错误:

  

错误33最佳重载方法匹配   'DocketViewer.GetDivDriverLicence(int,Employee)'有一些   无效的论点
  错误34参数2:无法转换   'System.Data.DataSet'到'Employee'

有搜索员工的代码:

 DataSet ds = Lookups.Employee.GetEmployee(Company.Current.CompanyID, JobID);
 int MainCount = 1;
 foreach (DataTable table in ds.Tables)
 {
     foreach (DataRow dr in table.Rows)
     {
         ulPODS.Controls.Add(GetLi("<a href=\"#DriverLicence-" + MainCount + "\">Driver Licence-" + ds.Tables[0].Rows[0]["ID"].ToString() + "</a>"));

         tabsPOD.Controls.Add(GetDivDriverLicence(MainCount, ds)); //error here
         MainCount++;
     }
 }

显示图像的功能:

 protected HtmlGenericControl GetDivDriverLicence(int Count,  Employee em)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Image");
    dt.Columns.Add("ID");
    dt.Columns.Add("Title");
    DataRow desRow = dt.NewRow();
    desRow["Image"] = em.DriverLicenseScanImage;
    desRow["ID"] = em.ID;
    desRow["Title"] = em.EmployeeNum;
    dt.Rows.Add(desRow);
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.ID = "Driver Licence-" + Count;
    FormView fv = new FormView();
    fv.ID = "FormViewDriverLicence-" + Count;
    fv.ItemTemplate = FormView.ItemTemplate;
    fv.ItemCommand += new FormViewCommandEventHandler(fv_ItemCommand);
    fv.DataBound += (sender, em) => { fv_DataBound(sender, em, "driverlicence"); };
    fv.DataSource = dt;
    fv.DataBind();
    div.Controls.Add(fv);
    return div;
}

2 个答案:

答案 0 :(得分:4)

您的问题是因为GetDivDriverLicence(int Count, Employee e)Employee作为其第二个参数,并且您将其传递给DataSet

tabsPOD.Controls.Add(GetDivDriverLicence(MainCount, ds)); // <--- ds is a DataSet, not an Employee.

更大的问题是:您在Employee方法中使用GetDivDriverLicence(int Count, Employee e)实例尝试做什么是什么?这只是数据集中只有一个字段可以做的事吗?您是否有从数据表中的行构建Employee实例的方法?

编辑:根据您的评论,以下是您可能添加到foreach循环的示例:

var emp = new Employee((int) dr["ID"]);
emp.Name = dr["Name"].ToString();
emp.Age = short dr["Age"];
tabsPOD.Controls.Add(GetDivDriverLicence(MainCount, emp)); 

答案 1 :(得分:1)

一个想法是这样的,你必须编写GetEmployeeFromDataSet函数来进行转换,虽然我怀疑这会编译:

DataSet ds = Lookups.Employee.GetEmployee(Company.Current.CompanyID, JobID);
int MainCount = 1;
foreach (DataTable table in ds.Tables)
{
  foreach (DataRow dr in table.Rows)
  {
     ulPODS.Controls.Add(GetLi("<a href=\"#DriverLicence-" + MainCount + "\">Driver Licence-" + ds.Tables[0].Rows[0]["ID"].ToString() + "</a>"));
     Employee emp = GetEmployeeFromDataSet(ds);
     tabsPOD.Controls.Add(GetDivDriverLicence(MainCount, emp));
     MainCount++;
  }
}

...

Employee GetEmployeeFromDataSet(DataSet ds) {
  Employee emp = new Employee();
  // convert the data from the ds into the newly made emp.
  return emp;
}