输入字符串格式不正确,预期类型为Int32

时间:2017-06-20 07:26:11

标签: c#

如何解决此错误 输入字符串格式不正确。不能存储在EmpID列中。预期类型是Int32

 DataSet info = this.GetData.getInfo("SELECT 0 AS ErNo,Er.EmpID,(Er.FName&' '&Er.MName&' '&Er.LName) AS [Employee Name], ss.DeptNo AS [Dept No] FROM (EmpReg AS Er INNER JOIN EmployeeDept AS ss ON Er.EmpId = ss.EmpId) WHERE ss.DeptId=" + (object)DeptID + " AND ss.Status=0 AND Er.EmpStatus=1 ORDER BY " + str, "EmployeeDept");
 var tbl = info.Tables[0];   
 tbl.Rows.Add(new object[] { 
      index, 
      tbl.Rows[index]["Employee Name"].ToString(), 
      tbl.Rows[index]["Dept No"].ToString(), 
      tbl.Rows[index]["EmpId"] 
    };

1 个答案:

答案 0 :(得分:1)

tbl中的列顺序在SELECT语句中定义。因此,您的表格包含以下列:

  • ErNo(作为int)
  • EmpID(我假设它是一个int)
  • 员工姓名(字符串)
  • 部门否(我认为它是一个int)

接下来,您尝试添加新行,但是您输入的数据值的顺序是不正确的顺序。此外,您正在尝试将已经是正确类型的字段转换为字符串。试试这个:

tbl.Rows.Add(new object[] { 
  index, 
  tbl.Rows[index]["EmpId"] // This field is moved two fields to the left / up.
  tbl.Rows[index]["Employee Name"], // Without the ToString.
  tbl.Rows[index]["Dept No"], // Without the ToString.
};