1
Reply

help and idea about code snippet

sadi

sadi

Feb 5 2011 1:27 PM
11.9k

my codes are below. what i want to do is:i return datas from DB using SetListData method. i want the records return as List format not Dataset.i send 2 paramter to SetListData method. storedprroc name and object that will return  lists. For example category class has id,catname properties. i want to do all listing records processes in one place. So, i try to use this method.this method doesn't give error. but it show records as object.how can i convert list<object> to class that i sent. (i call SetListData from another class lik this:
List<object> iller = Business.AdminBusiness.SetListData("sp_listil", new il());//il is a class that has properties id,name

return iller;
)
public
static List<object> SetListData(string procname, object sender)
{
Type tip = sender.GetType();
System.Reflection.PropertyInfo[] ozellikler = tip.GetProperties();


DBOperations dbop = new DBOperations();
SqlDataReader reader=dbop.DataReader(procname,null);
List<object> liste = new List<object>();
while (reader.HasRows && reader.Read())
{

liste.Add(CreateSenderObject(sender,reader));

}
return liste;
}

static object CreateSenderObject(object sender,IDataRecord rd)
{
Type tip = sender.GetType();
System.Reflection.PropertyInfo[] ozellikler = tip.GetProperties();
for (int i = 0; i < ozellikler.Length; i++)
{
if(ozellikler[i].PropertyType.ToString()=="System.String")
ozellikler[i].SetValue(sender,rd[i].ToString(),null);
else if(ozellikler[i].PropertyType.ToString()=="System.Int32")
ozellikler[i].SetValue(sender,Convert.ToInt32(rd[i].ToString()),null);
else if(ozellikler[i].PropertyType.ToString()=="System.DateTime")
ozellikler[i].SetValue(sender,Convert.ToDateTime(rd[i].ToString()),null);
else if(ozellikler[i].PropertyType.ToString()=="System.Decimal")
ozellikler[i].SetValue(sender,Convert.ToDecimal(rd[i].ToString()),null);
else if (ozellikler[i].PropertyType.ToString() == "System.Int32")
ozellikler[i].SetValue(sender, Convert.ToInt32(rd[i].ToString()), null);
}
return ozellikler;
}

Answers (1)