Wednesday, October 15, 2008

Convert Object Array To Dataset

Bind an Object Array to a dataset using the following code.

Code Snippet

using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace BusinessObject.Util
{
///
/// Utility Class for Object Arrays.
///

public class ObjectArray
{
private Object[] _objectArray;

public ObjectArray(Object[] objectArray)
{
this._objectArray = objectArray;
}

public DataSet ToDataSet()
{
DataSet ds = new DataSet();

XmlSerializer xmlSerializer =
new XmlSerializer(_objectArray.GetType());
StringWriter writer = new StringWriter();
xmlSerializer.Serialize(writer, _objectArray);
StringReader reader =
new StringReader(writer.ToString());

ds.ReadXml(reader);
return ds;
}
}
}

How to use the code:

Person[] personList;

personList = Course.GetPersonList();

ObjectArray objectArray = new ObjectArray(personList);Create a DataSet and convert your ObjectArray to a DataSet using the method:

DataSet ds = new DataSet();
ds = objectArray.ToDataSet();

Reference Taken From
http://tom.gilki.org/programming/net/Utils/ObjectArrayDataSet/index.shtml

This entry was posted on February 21, 2007 at 9:44 am and is filed under .NET 1.1.