Monday, March 12, 2012

How to convert Class object into DataSet?

Lets say, I have a following XSD utility generated class as follows:

[XmlRoot(Namespace="http://example.com/2007/ex1")]
public class Example {
public Example () {}
public string Name;
public int Power;
}

I have to pull data from several tables(MS Access2003) and store it in a DataSet.

Now, Is it possible to use it like:

Example example = new Example();
example.Power = 42;
example.Name = "Kibo";
DataSet myDataSet = new DataSet();
myDataSet = example;

or

myDataSet.Add(example);

Somehow, to add the example object tree in myDataSet, and pull values from tables directly into myDataSet.

This will help me in doing following things:

1. I'd not need to change my existing XSD generated class structure into DataSet.

2. Pull data directly into the DataSet, using explicit queries

3. Generate the Xml file directly.

Any help/idea/pointer/etc would help Alot.

Thanks.

The sample code you have given shouldn't work cause a DataSet can't load data from an custom entity object.

Check this site for more details:

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

Labels: , , , , , , , , , , , , , , ,

how to convert a row in gridview to my business class DialUpRow generated by dataSet desig

I have GridView with: objectdatasource="objectdatasource1"

Objectdatasource1 is linked to my BLL (business layer with a dataset designer genrating: DialUpRow as an item of my datatable: DialupUpDataTable

I want to convert the selected row to my class: DialUpRow

I get the selected row using: myGRidView.SelectedIndex

But I don t know how to convert the grid row to my DialUpRow in order to get intellisence of my row properies (calss properties in code behind)

DataItem property of the GridViewRow would give the dataitem bound to the GridView. Below is a sample code. In the code, I haven't done any error checking, please ensure you do in your code.

Dim row as GridViewRow = GridView1.Rows(GridView1.SelectedIndex)if row isnot nothing thenDim drow as DialUpRow =CType(row.DataItem, DialUpRow)endif

Labels: , , , , , , , , , , , , , , , , ,