In Physical Science (if you did it to grade nine), you would have learnt that you cannot lose energy – you can only convert the energy to another form. For example, when a fire burns, it gives off heat. That heat is then transferred into a light energy.
Similarly, when you develop a control that data source that is given should be converted to an offline version. The only reason for the data source to be converted to an offline version is so that during post backs, that data can be used by your control to re-populate itself.
ADO.NET fits perfectly into this situation because, in summary, ADO.NET provides an offline abstract view of the data. Specifically, the DataTable will be best to be utilized because it is easily serialized into an XML mark-up (text).
So, what I have told you so far, you will need to do the following:
- Get an object in the data source that implements the ICollection interface.
- Convert the data source into a DataTable so that it may be used for every post back.
However, I have to look at my review of what must be done and pause at point one and look at point two… then the thought that the page developer might want to feed us a data reader. “Oh no,” one might exclaim.
That does not really affect us because we can skip straight into point two. We can cleverly convert a data reader into a DataTable within a flash. The code sample listed below takes the data reader and finds a DataTable make-up (that is, a schema table):
public object DataSource {
get { return ViewState["DataSource"]; }
set {
if (DataSource is DataTable)
ViewState["DataSource"] = value;
else if (value is IDataReader) {
IDataReader reader = (IDataReader)value;
// gets the column based table (schema)
DataTable table = reader.GetSchemaTable();
while (reader.Read()) {
DataRow row = table.NewRow();
table.Rows.Add(row);
foreach (DataColumn column in table.Columns)
row[column.ColumnName] = reader[column.ColumnName];
}
DataSource = table;
}
}
}