简体   繁体   中英

ASP.Net Code block not returning data

I have a code block that does not return the expected data values.

protected void Page_Load(object sender, EventArgs e)
{
corpEmployee.Employee editEmp = new corpEmployee.Employee();

editEmp.EmployeeID = PatientCustomerID.Value;
corpCustomerMgr.GetEmployeeRecord(editEmp);

tboxFirstName.Text = editEmp.EmpFirstName.ToString();
tboxLastName.Text = editEmp.EmpLastName.ToString();
tboxCity.Text = editEmp.EmpCity.ToString();
tboxAddress.Text = editEmp.EmpAddrLine1.ToString();
}  


public static void GetEmployeeRecord(corpEmployee.Employee QueryData)
{
    try
    {
        List<corpEmployee.Employee> empRecord = new List<corpEmployee.Employee>();
        corpCustomerDAL.GetEmployeeData(empRecord, QueryData);
    }
    catch (Exception ex)
    {
    LogAppError(ex.ToString());
    }
}  

When corpCustomerDAL.GetEmployeeData(empRecord, QueryData); is executed, empRecord is returned with the Employee object with correct property values. However, when the code comes back to corpCustomerMgr.GetEmployeeRecord(editEmp); the employee object has null values.

How can I get the Employee object values back to the Page_Load routine?

You could either return the object back in the GetEmployeeRecord static method or you could include ref in front of your arguments so that you are passing the employee in as a reference instead of copying the variable.

I would recommend returning your data back vs using ref as your method name seems misleading, among other reasons.

Based on your comments, it looks like you are populating empRecord with the employee data you require. The simplest option is to return the populated record from GetEmployeeRecord :

public static corpEmployee.Employee GetEmployeeRecord(corpEmployee queryData)
{
    List<corpEmployee.Employee> empRecord = new List<corpEmployee.Employee>();
    corpCustomerDAL.GetEmployeeData(empRecord, QueryData);
    return empRecord.Count == 0 ? null : empRecord[0];  //or empRecord.FirstOrDefault()
}

You should then change the start of your Page_Load handler to:

corpEmployee.Employee queryEmp = new corpEmployee.Employee();
queryEmp.EmployeeID = PatientCustomerID.Value;

corpeEmployee.Employee editEmp = corpCustomerMgr.GetEmployeeRecord(queryEmp);

Two immediate thoughts occur:

  • Your exception handling needs work. You shouldn't be catching Exception , and you shouldn't be silently swallowing exceptions at all. It could well be that an exception is being thrown, and that's why you're not getting the data.

  • You've got a Get*** method - that should be returning data. It appears you're expecting the results to be put into an object... that's a confusing way of getting data out of a method. It would be clearer if your method signature were something like:

     public static Employee GetEmployeeRecord(string employeeId) 

Most likely what's happening is that your employee object that you're passing in is being copied, and is destroyed when the function terminates. To better illustrate, I've added some comments:

public static void GetEmployeeRecord(corpEmployee.Employee QueryData)
{
    //QueryData is a newly created Employee here, and is NOT the same one that was passed in.
    try
    {
        List<corpEmployee.Employee> empRecord = new List<corpEmployee.Employee>();
        corpCustomerDAL.GetEmployeeData(empRecord, QueryData);//
        //QueryData now contains the data you want your original object to contain
    }
    catch (Exception ex)
    {
    LogAppError(ex.ToString());
    }
}  //when this function terminates, QueryData ceases to exist.

In order to fix it, pass the employee by reference (use the ref keyword) instead of passing it by value, which is what you're doing right now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM