I have two DataSets with identical tables.
DataSet1
DataSet2
"FullName" column of both tables have expression property set as
[Grade]+'-'+[Class]
For DataSet2 I have defined a method - "Import".
// DataSet2.cs file
namespace ConsoleApplication1
{
public partial class DataSet2
{
public void Import(DataSet1 dataSet1)
{
this.Group.Load(dataSet1.Group.CreateDataReader());
}
}
}
The last line of code below (dataSet2.Import(dataSet1);
) is throwing InvalidOperationException.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataSet1 dataSet1 = new DataSet1();
dataSet1.Group.AddGroupRow(1, "A");
dataSet1.Group.AddGroupRow(1, "B");
DataSet2 dataSet2 = new DataSet2();
dataSet2.Import(dataSet1);
}
}
}
That's because "FullName" column is computed column. I'd like to know how to bypass this exception in this particular situation? Is it possible to make the DataReader skip computed columns while loading? Or maybe there are some other "tricks"?.
This is a reproduction of a problem in one of my projects with DataSets, with several Tables in them.
Do not use CreateDataReader
, instead use WriteXml
and ReadXml
:
var ds1 = new DataSet1();
ds1.DataTable1.Rows.Add(1, "One");
ds1.DataTable1.Rows.Add(2, "Two");
var ds2 = new DataSet2();
using(var s= new MemoryStream())
{
ds1.DataTable1.WriteXml(s);
s.Seek(0, SeekOrigin.Begin);
ds2.DataTable1.ReadXml(s);
}
In above method, since I've used the same stream for write and read, I need to reset the stream poition, so the read statement can read from beginning.