Showing posts with label dataset. Show all posts
Showing posts with label dataset. Show all posts

Monday, March 26, 2012

Serializable

What does "DataSet is Serializable" mean?

Ronhttp://en.wikipedia.org/wiki/Serialization
It means that the DataSet can be persisted to some medium. A file is one
example.

You can save (serialize) the DataSet to a file. Then , some amount of time
later (after reboots, whatever) , you can come back and "reclaim" it from
the file.

Look up

WriteXml
ReadXml

methods under dataset.

<rn5a@.rediffmail.comwrote in message
news:1191813249.729920.67930@.v3g2000hsg.googlegrou ps.com...

Quote:

Originally Posted by

What does "DataSet is Serializable" mean?
>
Ron
>

serializable class

Hi,

How do you make a class generated by the DataSet.xsd serializable? So I can store it in session with SQL Server?

Thanks.

You can mark the class as serilizable like this:

[Serializable]
public class MyClass

{

//Class code...

}


Thanks. But the class I am trying to Serialized is generated by DataSet.xsd, so I cannot do that.

I have tried to make a partial class but it does not work:

[Serializable]

public partial class DataSetXSDClassName {

}


OH! Sorry... I missed the part of the xsd... Can you post the class generated by it then?


I am not sure about that but you can do the followin work around.

Provided your generated class look like this:

public class A{public int id;public string titlepublic A(int aId,string aTitle) { id = aId; title = aTitle; }}//then you define class B like this:[Serializable]public class B{public int id;public string title;public B(A obj) { id = obj.id; title = obj.title; }}
after that you can serialize and deserialize instances of B class and then use their members/properties to create A object and further use it in your code. Other benefit of it is that you can serialize only the information that you need.


The class is not actually 'physical' that I can go and copy and paste the code, but if any error occur then it will show up in debug mode, but will a weird filename like like random string.

Also the 'work around' class does work, but that will generated a class 99% the same as the one from xsd file. Any changes in dataset.xsd means I manually have to do the same for this 'work around' class.

But thanks for the comment.

Saturday, March 24, 2012

Serializing classes derived from dataset

Hi,

I am trying to store various objects within session. Due
to the site being hosted in a web farm we are going to
have to use either State Server or SQL Server to persist
the data in session. All of the objects are classes which
inherit from a typed dataset.

Looking at MSDN I have found the following two (what I
believe to be) relevent statements (from article Object
Serialization in the .NET Framework):

"Derived classes should call the GetObjectData method on
the base object if the latter implements ISerializable"

and

"When you derive a new class from one that implements
ISerializable, the derived class must implement both the
constructor as well as the GetObjectData method if it has
any variables that need to be serialized."

Since the Dataset implements ISerializable the above
statements indicate that I should override the
GetObjectData method. However if I try and override this
method I get a compile error stating:

"sub 'GetObjectData' cannot be declared 'Overrides'
because it does not override a sub in a base class."

I am also unable to call the method GetObjectData on the
base class as it is not accessible.

The classes which I am trying to persist have their own
properties, some of which are themselves objects which
inherit from datasets.

If I create a test class which inherits from a typed
dataset, and which has two additional properites (named
Property1 and Property2) the following XML is persisted:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="dsTemp"
targetNamespace="http://tempuri.org/dsTemp.xsd"
xmlns:mstns="http://tempuri.org/dsTemp.xsd"
xmlns="http://tempuri.org/dsTemp.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="dsTemp" msdata:IsDataSet="true"
msdata:Property1="1" msdata:Property2="Aidan">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="InvoiceHeader">
<xs:complexType>
<xs:sequence>
<xs:element name="InvoiceHeaderID"
msdata:ReadOnly="true" msdata:AutoIncrement="true"
type="xs:int" />
<xs:element name="POHeaderID" type="xs:int"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="dsTempKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:InvoiceHeader" />
<xs:field xpath="mstns:InvoiceHeaderID" />
</xs:unique>
</xs:element>
</xs:schema
Note that the properties have been serialized, but have
been serialized as "<xs:element name="dsTemp"
msdata:IsDataSet="true" msdata:Property1="1"
msdata:Property2="Aidan">"

I can find no way in which I can de-serialize these fields.

Can someone please explain how I can have a class which
inherits from a dataset, and which I can then serialize
and de-serialize without losing the properties.

Thanks in advance

AidanNote that the xml which should have been displayed
indicated that the properties were included in a dataset
element as

msdata:Property1="1"
msdata:Property2="Aidan"

>--Original Message--
>Hi,
>I am trying to store various objects within session. Due
>to the site being hosted in a web farm we are going to
>have to use either State Server or SQL Server to persist
>the data in session. All of the objects are classes
which
>inherit from a typed dataset.
>Looking at MSDN I have found the following two (what I
>believe to be) relevent statements (from article Object
>Serialization in the .NET Framework):
>"Derived classes should call the GetObjectData method on
>the base object if the latter implements ISerializable"
>and
>"When you derive a new class from one that implements
>ISerializable, the derived class must implement both the
>constructor as well as the GetObjectData method if it has
>any variables that need to be serialized."
>Since the Dataset implements ISerializable the above
>statements indicate that I should override the
>GetObjectData method. However if I try and override this
>method I get a compile error stating:
>"sub 'GetObjectData' cannot be declared 'Overrides'
>because it does not override a sub in a base class."
>I am also unable to call the method GetObjectData on the
>base class as it is not accessible.
>The classes which I am trying to persist have their own
>properties, some of which are themselves objects which
>inherit from datasets.
>If I create a test class which inherits from a typed
>dataset, and which has two additional properites (named
>Property1 and Property2) the following XML is persisted:
><?xml version="1.0" encoding="utf-16"?>
><xs:schema id="dsTemp"
>targetNamespace="http://tempuri.org/dsTemp.xsd"
>xmlns:mstns="http://tempuri.org/dsTemp.xsd"
>xmlns="http://tempuri.org/dsTemp.xsd"
>xmlns:xs="http://www.w3.org/2001/XMLSchema"
>xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
>attributeFormDefault="qualified"
>elementFormDefault="qualified">
> <xs:element name="dsTemp" msdata:IsDataSet="true"
>msdata:Property1="1" msdata:Property2="Aidan">
> <xs:complexType>
> <xs:choice maxOccurs="unbounded">
> <xs:element name="InvoiceHeader">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="InvoiceHeaderID"
>msdata:ReadOnly="true" msdata:AutoIncrement="true"
>type="xs:int" />
> <xs:element name="POHeaderID" type="xs:int"
>minOccurs="0" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:choice>
> </xs:complexType>
> <xs:unique name="dsTempKey1" msdata:PrimaryKey="true">
> <xs:selector xpath=".//mstns:InvoiceHeader" />
> <xs:field xpath="mstns:InvoiceHeaderID" />
> </xs:unique>
> </xs:element>
></xs:schema>
>Note that the properties have been serialized, but have
>been serialized as "<xs:element name="dsTemp"
>msdata:IsDataSet="true" msdata:Property1="1"
>msdata:Property2="Aidan">"
>I can find no way in which I can de-serialize these
fields.
>Can someone please explain how I can have a class which
>inherits from a dataset, and which I can then serialize
>and de-serialize without losing the properties.
>Thanks in advance
>Aidan
>.

Thursday, March 22, 2012

Serious limitation of ObjectDataSource + DataObjectTypeName + DataSet as BusinessEntity

Hi fellas,

has anyone managed to get ObjectDataSource to work Datasets where the
Dataset is used as BusinessEntity class?

What I mean is the following architecture:

1.WebPage with ObjectDataSource
2. CustomBusinessObject which
* does validation,
* uses a Strongly Typed DataTable as a collection of BusinessEntity
items
* forwards data access to DataAdapters as a data layer
3. DataLayer based on DataAdapters

Ideally I would like to use the DataObjectTypeName property of the
ObjectDataSource
so that the methods of the CustomBusinessObject are with a single
parameter e.g.
Insert (Customer c);
The problem stems from the fact that the ObjectDataSouce requires the
BusinessEntity class (in this case this would be strongly typed data
row) should have a default constructor. Unfortunately, I could not
figure way of implementing a strongly typed data row with default
constructor.

Is there any way around this situation?

The only solution I could come up with so far is to not set the
DataObjectTypeName and handle the ObjectDataSource's Inserting,
Updating and Deleting events ... but this still requires a fair amount
of trivial code.

It would have been great if there was a way to plug into the creation
of BusinessEntity instances instead of relying on default constructors
...

Cheers,

MillenThe ObjectDataSource isn't designed with extensibility in mind so you
have to adapt to it.

If you need more customization, you can use my ExtendedObjectDataSource
that has more features and comes with full source so you can modify
what you want:

http://www.manuelabadia.com/products/EODS_features.aspx

Right now to create the object it uses
Activator.CreateInstance(dataObjectType) but you can change it to
whatever you need.

Manu