I'd like to return in BLLServiceRequests the DALServiceRequest properties along with its corresponding DALRequest properties since the SP I used is a JOIN table between Request and ServiceRequest tables. The problem is when I hit this code:
var requestNewProjects = Common.ToList(dt);
It can't find the DALRequest properties I've set in DALServiceRequest's properties. Could someone point out to me the correct way to implement this? Thank you.
BLL
public class BLLServiceRequests
{
public List<DALServiceRequests.Properties> GetServiceRequestByRequestId(intrequestID)
{
var obj = new DALServiceRequests();
return obj.GetServiceRequestByRequestId(requestID);
}
}
DAL
public class DALRequests
{
public class Properties
{
public int id {get;set;}
public int requestType { get; set; }
public DateTime createDate { get; set; }
}
}
public class DALServiceRequests
{
public class Properties
{
public int id { get; set; }
public int requestId { get; set; }
public string Issue { get; set; }
public DALRequests.Properties request { get; set; } //Is this correct?
}
public List<DALServiceRequests.Properties> GetServiceRequestByRequestId(int requestID)
{
string cnStr = Common.dbConnStr;
SqlConnection cn = new SqlConnection(cnStr);
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetServiceRequestByRequestId";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@requestID", requestID);
try
{
cn.Open();
cmd.Connection = cn;
adapter.SelectCommand = cmd;
adapter.Fill(dt);
var requestNewProjects = Common.ToList<DALServiceRequests.Properties>(dt);
//Code above can't find the DALRequests properties which is already defined in DALServiceRequests.Properties
return requestNewProjects;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
cn.Dispose();
cmd.Dispose();
dt.Dispose();
adapter.Dispose();
}
}
}
Here's my Common class in DAL. Nothing to see here it's just a utility class I use to convert datatable to list
public static class Common
{
public static string dbConnStr = ConfigurationManager.ConnectionStrings["dbConnString"].ToString();
public static List<T> ToList<T>(DataTable table)
{
List<T> list = new List<T>();
T item;
Type listItemType = typeof(T);
for (int i = 0; i < table.Rows.Count; i++)
{
item = (T)Activator.CreateInstance(listItemType);
MapRow(item, table, listItemType, i);
list.Add(item);
}
return list;
}
private static void MapRow(object vOb, DataTable table, Type type, int row)
{
for (int col = 0; col < table.Columns.Count; col++)
{
var columnName = table.Columns[col].ColumnName;
var prop = type.GetProperty(columnName);
object data = table.Rows[row][col];
if (data == System.DBNull.Value)
{
}
else
prop.SetValue(vOb, data, null);
}
}
}
For this you can use inheritance.
If you add
DALRequeststoDALServiceRequestsit will give you acces to all the properties insideDALRequestsincluding all methods but as i see you only have properties inDALRequestsAnd a other way would be to just define the class as a property as you did before but without the
.propertiesThen you can acces the properties in the following way: