我有一个表 empinf 字段
Empid int
EmpName Nvarchar
Salary Numeric
我有一个包含方法 Getdetails 的 EmployeeUtility 类
public EmployeeDetails Getdetails(int employeeId)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SelectEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Id",SqlDbType.Int,4));
cmd.Parameters["@Id"].Value=employeeId;
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.HasRows) return null;
reader.Read();
EmployeeDetails emp = new EmployeeDetails((int)reader["EmpId"], (string)reader["EmpName"], (int)reader["Salary"]);
reader.Close();
return emp;
}
catch (SqlException e)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
}
EmployeeDetails emp=new EmployeeDetails((int)reader["EmpId"],(string)reader["EmpName"],(int)reader["Salary"]);
它在这一行显示一个错误,表明Specified cast is not valid.
请需要帮助。
我的employeeDetails 类如下:
public class EmployeeDetails
{
private int employeeID;
public int EmployeeID
{
get { return employeeID; }
set { employeeID = value; }
}
private string employeeName;
public string EmployeeName
{
get { return employeeName; }
set { employeeName = value; }
}
private int salary;
public int Salary
{
get { return salary; }
set { salary = value; }
}
public EmployeeDetails(int employeeId, string employeeName, int salary)
{
EmployeeID = employeeID;
EmployeeName = employeeName;
Salary = salary;
}
public EmployeeDetails()
{
// TODO: Complete member initialization
}
}