Reputation: 440
I have three tables. [C# windows form, ms sql server]
CustomerTable:
------Id
------Name
DressTypeTable
------DressTypeId
------Name
MeasurementDetailsTable
------Id
------DressTypeId
------Details
------CustomerId
I want to retrieve [ using customerId]
Customer Name
DressType Name
Measurement Details.
I'm using this query below:
_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;
When I want to retrieve value using CustomerId it shows "Object reference set to an instance of an object". It can not retrieve data from
_aMeasurementDetails.DressType.DressTypeId = (int)aReader[1];
and
_aMeasurementDetails.Customer.CustomerId = (int)aReader[3];
But when I'm executing the query in the ms sql server, it can retrieve the data! How to solve this problem?
Here is my MeasurementDetails Model Class
public int MeasurementDetailsId { get; set; }
public String Details { get; set; }
public DressType DressType { get; set; }
public Customer Customer { get; set; }
Here is my 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);
}
Here is my MeasurementDetails Gateway class
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;
}
Upvotes: 0
Views: 109
Reputation: 11635
The problem you are having is that the class you are instantiating sets its properties to their respective default values. If you examine _aMeasurementDetails
in the debugger after this line:
_aMeasurementDetails = new MeasurementDetails();
You will see that DressType
and Customer
are null
.
Since the default value for DressType
and Customer
is null
you cannot access their respective properties. You have to instantiate these properties before accessing them, which can be done in some different ways.
Via constructor:
public class MeasurementDetails{
....
public MeasurementDetails(){
Customer = new Customer();
DressType = new DressType();
....
}
}
via the class that uses MeasurementDetails
_aMeasurementDetails = new MeasurementDetails();
_aMeasurementDetails.Customer = new Customer();
_aMeasurementDetails.DressType = new DressType();
or you can initialize it when you're fetching the values.
MeasurementDetails _aMeasurementDetails;
_aMeasurementDetails = new MeasurementDetails {
MeasurementDetailsId = (int)aReader[0],
DressType = { DressTypeId = (int)aReader[1] },
Details = (string)aReader[2],
Customer = { CustomerId = (int)aReader[3] }
}
Upvotes: 2