为什么这些值显示为空?

时间:2014-08-21 11:10:16

标签: c# sql-server winforms

我有三张桌子。 [C#windows form,ms sql server]

CustomerTable的:

------编号

------名称

DressTypeTable

------ DressTypeId

------名称

MeasurementDetailsTable

------编号

------ DressTypeId

------详细

------客户ID

我想检索[使用customerId]

客户名称

DressType名称

测量细节。

我在下面使用此查询:

 _aMeasurementDetails = new MeasurementDetails();
        _connection.Open();
        string query = string.Format("SELECT * FROM MeasurementDetailsTable where CustomerId='{0}'", id);
        _command = new SqlCommand(query, _connection);
        SqlDataReader aReader = _command.ExecuteReader();


        if (aReader.HasRows)
        {
            while (aReader.Read())
            {
                _aMeasurementDetails.MeasurementDetailsId = (int)aReader[0];
                _aMeasurementDetails.DressType.DressTypeId = (int)aReader[1];
                _aMeasurementDetails.Details = (string)aReader[2];
                _aMeasurementDetails.Customer.CustomerId = (int)aReader[3];
            }
        }
        _connection.Close();
        return _aMeasurementDetails;

当我想使用CustomerId检索值时,它显示"对象引用设置为对象的实例"。它无法从

中检索数据
 _aMeasurementDetails.DressType.DressTypeId = (int)aReader[1];

  _aMeasurementDetails.Customer.CustomerId = (int)aReader[3];

但是当我在ms sql server中执行查询时,它可以检索数据!如何解决这个问题呢?

这是我的MeasurementDetails模型类

 public int MeasurementDetailsId { get; set; }
    public String Details { get; set; }
    public DressType DressType { get; set; }

    public Customer Customer { get; set; }

这是我的MeasurementDetails Manager Class

 private MeasurementDetailsGateway aMeasurementDetailsGateway;
    public bool SaveMeasurementDetails(Model.MeasurementDetails aMeasurementDetails)
    {
        aMeasurementDetailsGateway = new MeasurementDetailsGateway();
        return aMeasurementDetailsGateway.SaveMeasurementDetails(aMeasurementDetails);
    }

    public Model.MeasurementDetails GetMeasurementDetailsUsingCustomerInfo(int id)
    {
        aMeasurementDetailsGateway = new MeasurementDetailsGateway();
        return aMeasurementDetailsGateway.GetMeasurementDetailsUsingCustomerInfo(id);
    }

这是我的MeasurementDetails网关类

private SqlConnection _connection;
    private SqlCommand _command;
    private MeasurementDetails _aMeasurementDetails;

    public MeasurementDetailsGateway()
    {
        _connection = new SqlConnection();
        _connection.ConnectionString = ConfigurationManager.ConnectionStrings["TailorShopDB"].ConnectionString;
    }
    public bool SaveMeasurementDetails(Model.MeasurementDetails aMeasurementDetails)
    {
        _connection.Open();
        string query = string.Format("INSERT INTO MeasurementDetailsTable values ('{0}','{1}', '{2}')", 
            aMeasurementDetails.DressType.DressTypeId, aMeasurementDetails.Details, aMeasurementDetails.Customer.CustomerId);

        _command = new SqlCommand(query, _connection);
        int affectedRows = _command.ExecuteNonQuery();
        _connection.Close();
        if (affectedRows > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public Model.MeasurementDetails GetMeasurementDetailsUsingCustomerInfo(int id)
    {

        _aMeasurementDetails = new MeasurementDetails();
        _connection.Open();
        string query = string.Format("SELECT * FROM MeasurementDetailsTable where CustomerId='{0}'", id);
        _command = new SqlCommand(query, _connection);
        SqlDataReader aReader = _command.ExecuteReader();


        if (aReader.HasRows)
        {
            while (aReader.Read())
            {
                _aMeasurementDetails.MeasurementDetailsId = (int)aReader[0];
                _aMeasurementDetails.DressType.DressTypeId = (int)aReader["DressTypeId"];
                _aMeasurementDetails.Details = (string)aReader[2];
                _aMeasurementDetails.Customer.CustomerId = (int)aReader["CustomerId"];
            }
        }
        _connection.Close();
        return _aMeasurementDetails;
    }

1 个答案:

答案 0 :(得分:2)

您遇到的问题是您要实例化的类将其属性设置为各自的default values。如果您在此行之后检查调试器中的_aMeasurementDetails

_aMeasurementDetails = new MeasurementDetails();

您会看到DressTypeCustomernull

由于DressTypeCustomer的默认值为null,因此您无法访问其各自的属性。您必须在访问它们之前实例化这些属性,这可以通过某些不同的方式完成。

通过构造函数:

public class MeasurementDetails{
    ....
    public MeasurementDetails(){
        Customer = new Customer();
        DressType = new DressType();
        ....
    }
}

通过使用MeasurementDetails

的班级
_aMeasurementDetails = new MeasurementDetails();
_aMeasurementDetails.Customer = new Customer();
_aMeasurementDetails.DressType = new DressType();

或者您可以在获取值时初始化它。

MeasurementDetails _aMeasurementDetails;

_aMeasurementDetails = new MeasurementDetails {
    MeasurementDetailsId = (int)aReader[0],
    DressType = { DressTypeId = (int)aReader[1] },
    Details = (string)aReader[2],
    Customer = { CustomerId = (int)aReader[3] }
}