trouble in converting Generic List coming from a WCF Service to a DataTable -


i confused on how can use generic methods parse generic list datatable/dataset. setup: 1. have class customers defined in wcf service library.

namespace wcf.sample.servicelibrary {     public class customers     {         public string id = string.empty;         public string companyname = string.empty;         public string contactname = string.empty;     } } 

2. use class return generic list operationcontract.

namespace wcf.sample.servicelibrary {     [servicecontract]     public interface icustomerservice     {         [operationcontract]         list<customers> getallcustomers();     }        } 

3. consume wcf service in web client page. on button click populate gridview list returned getallcustomers(). works fine.

gridview1.datasource = client.getallcustomers(); gridview1.databind(); 

4. issue is, reason (sort/paging function) want convert returned generic list datatable. so, have method returns me datatable want bind gridview. here methods:

public static datatable convertto<t>(system.collections.generic.list<t> genericlist) {     //create datatable structure     datatable datatable = createtable<t>();     type enttype = typeof(t);     propertydescriptorcollection properties = typedescriptor.getproperties(enttype);     //get list item , add list     foreach (t item in genericlist)     {         datarow row = datatable.newrow();         foreach (propertydescriptor prop in properties)         {             row[prop.name] = prop.getvalue(item);         }         datatable.rows.add(row);     }     return datatable; }  public static datatable createtable<t>() {     //t –> classname     type enttype = typeof(t);     //set datatable name class name     datatable datatable = new datatable(enttype.name);     //get property list     propertydescriptorcollection properties = typedescriptor.getproperties(enttype);     foreach (propertydescriptor prop in properties)     {         //add property column         datatable.columns.add(prop.name, prop.propertytype);     }     return datatable; } 

i not sure how call function? how can specify customers class in webservice? totally lost. appreciate if can guide me on following code, how make work.

gridview1.datasource = convertto<???>(client.getallcustomers()); 

i able resolve issue modifing wcf service (although reluctant so). modified getallcustomers method return datatable instead of generic type. in service itself, converting generic type datatable using same methods:

public static datatable convertto<t>(system.collections.generic.list<t> genericlist)  {      //create datatable structure      datatable datatable = createtable<t>();      type enttype = typeof(t);      propertydescriptorcollection properties = typedescriptor.getproperties(enttype);      //get list item , add list      foreach (t item in genericlist)      {          datarow row = datatable.newrow();          foreach (propertydescriptor prop in properties)          {              row[prop.name] = prop.getvalue(item);          }          datatable.rows.add(row);      }      return datatable;  }   public static datatable createtable<t>()  {      //t –> classname      type enttype = typeof(t);      //set datatable name class name      datatable datatable = new datatable(enttype.name);      //get property list      propertydescriptorcollection properties = typedescriptor.getproperties(enttype);      foreach (propertydescriptor prop in properties)      {          //add property column          datatable.columns.add(prop.name, prop.propertytype);      }      return datatable;  } 

another thing noticed following line

propertydescriptorcollection properties = typedescriptor.getproperties(enttype); 

would returned null type. due fact didn't have get/set methods in customers class. created get/set methods in customer class , worked charm.

thanks helped , tried :)


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -