daljit
daljit

Reputation: 1266

C# : Make Dataset Serializable for use in SessiosnState Mode SqlServer

We have web based application, we used dataset to store the information. WE used to store these datasets in Sessions and get from Sessions during Ajax call and update them. At that time Session State was InProc.

Now We want to store the session data in Sql Sever, to store in session we need to make the dataset [Serializable]

I test with normal datasets it works fine, but in our case we create our DataSets by inherit from DataSet like

public class OurDataSet : DataSet
{
#constants
all the column names are used in all the tables are used as Constants.

#constructor
// add all the related tables
public OurDataSet()
{
this.Tables.Add(OurDataTable());
}
public static DataTable OurDataTable()
{
}
}

My problem is that i can not serialized this object even decorated with [Serializable] attribute.

  1. How i will be able to store this dataset in the SQLServer session mode and get it back.
  2. It serialize fine, but gives me the following error on getting the data back from sqlserver. The constructor to deserialize an object of type 'OurDataSet' was not found.

Thanks

Upvotes: 2

Views: 759

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

DataSet uses custom serialization (ISerializable) - which means you need to add a supporting constructor:

protected OurDataSet(SerializationInfo information, StreamingContext context)
    : base(information, context) {}

Note that the base implementation should handle all the details - you dont need to add anything. This may be in addition to a default constructor:

public OurDataSet() {}

Upvotes: 2

Related Questions