R
Radenko Zec
Hi i have generic list problem with DAL code.
class UlazTotal
{
private System.DateTime uT_DATUMField;
private string rA_SIFRAField;
private decimal uT_IZNOSField;
..........
public System.DateTime UT_DATUM
{
get
{return this.uT_DATUMField;}
set
{this.uT_DATUMField = value;}
}
public string RA_SIFRA
{
get
{return this.rA_SIFRAField;}
set
{this.rA_SIFRAField = value;}
}
public decimal UT_IZNOS
{
get
{return this.uT_IZNOSField;}
set
{this.uT_IZNOSField = value;}
}.........................
}
I want to make generic method for getting all data from any class in solution.
public static List<T> GetAll<T>(List<T> source) where T: new()
{
List<T> lista = new List<T>();
SqlParameter[] parameters =
{};
SqlDataReader dr = SqlHelper.ExecuteReader(NoviTemplate.SqlHelper.ConnStr(), CommandType.StoredProcedure, "sp_UlazTotal_GetAll");
while (dr.Read())
{
T obj;
obj=new T();
Read(obj, dr); /////////////////HERE I GET ERROR
}
dr.Close();
return lista;
}
private static void Read(UlazTotal ulazTotal, SqlDataReader dr)
{
UlazTotal ulazTotal = new UlazTotal() ;
ulazTotal.UT_DATUM = Convert.ToDateTime(dr["UT_DATUM"].ToString());
ulazTotal.RA_SIFRA = dr["RA_SIFRA"].ToString();
ulazTotal.UT_IZNOS = Convert.ToDecimal(dr["UT_IZNOS"].ToString());
...............
}
How to make Generic Read method that accept any class (not ulazTotal class) and populate properties of any class with data from reader?
Thanks
class UlazTotal
{
private System.DateTime uT_DATUMField;
private string rA_SIFRAField;
private decimal uT_IZNOSField;
..........
public System.DateTime UT_DATUM
{
get
{return this.uT_DATUMField;}
set
{this.uT_DATUMField = value;}
}
public string RA_SIFRA
{
get
{return this.rA_SIFRAField;}
set
{this.rA_SIFRAField = value;}
}
public decimal UT_IZNOS
{
get
{return this.uT_IZNOSField;}
set
{this.uT_IZNOSField = value;}
}.........................
}
I want to make generic method for getting all data from any class in solution.
public static List<T> GetAll<T>(List<T> source) where T: new()
{
List<T> lista = new List<T>();
SqlParameter[] parameters =
{};
SqlDataReader dr = SqlHelper.ExecuteReader(NoviTemplate.SqlHelper.ConnStr(), CommandType.StoredProcedure, "sp_UlazTotal_GetAll");
while (dr.Read())
{
T obj;
obj=new T();
Read(obj, dr); /////////////////HERE I GET ERROR
}
dr.Close();
return lista;
}
private static void Read(UlazTotal ulazTotal, SqlDataReader dr)
{
UlazTotal ulazTotal = new UlazTotal() ;
ulazTotal.UT_DATUM = Convert.ToDateTime(dr["UT_DATUM"].ToString());
ulazTotal.RA_SIFRA = dr["RA_SIFRA"].ToString();
ulazTotal.UT_IZNOS = Convert.ToDecimal(dr["UT_IZNOS"].ToString());
...............
}
How to make Generic Read method that accept any class (not ulazTotal class) and populate properties of any class with data from reader?
Thanks