Loading ...

Convert Generic list to Dataset

 /5
0 (0votes)

Yesterday I had a situation where I was calling a Web Service that returned an array of items and i needed to bind a reapter using list . So, after searching and trying different ways . I came up with the following solution to Convert the list into Dataset and than bind it with the reapter.

Here is code snippets -

Include the following namespace –

using System.Text;
using System.IO;
using System.Xml.Serialization;

 

public DataSet ConvertGenericsListToDataSet(ListDTO[] listvalue)

        {
            StringBuilder sb = new StringBuilder();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ListDTO[]));          

            StringWriter sw = new StringWriter(sb);

            xmlSerializer.Serialize(sw, listvalue);

            StringReader stream = new StringReader(sb.ToString());

            DataSet ds = new DataSet();

            ds.ReadXml(stream);

            return ds;

        }

  rptLisr.DataSource = ConvertGenericsListToDataSet(listvalue);

  rptLisr.DataBind();          

There you go!  A class to take any type of object that implements the ICollection interfaces and convert it to a Dataset.

Many thanks!!!!

Comments (1)

   
Jonno
ListDTO[] not highlighted and non-existing for me ???
9/18/2009
 · 
by

Top Posts