im trying to get datas from database dynamically. i've list<T> select<T>()
method... heres my issue i can get datas by using datareader here is code:
public list<T> Select<T>()
{
Type type = typeof(T);
...
...
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
try
{
SqlCommand sqlCommand = new SqlCommand(selectCommand, connection);
connection.Open();
SqlDataReader sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
foreach (PropertyInfo property in type.GetProperties())
{
property.SetValue( property.Name,(PropertyInfo)sqlReader[property.Name],null);
}
typeList.Add((T)Convert.ChangeType(type,typeof(T)));
}
sqlReader.Close();
}
catch (Exception ex)
{
string exc = ex.Message;
}
finally
{
connection.Close();
}
}
return typeList;
}
i can get data but i cannot assign it to my type so i cannot add my type into my typelist, can help me