The problem seems to be with a cloning a DataTable that has a column
expression that references its parent.

<xs:element name="DataFldSetName" msdata:ReadOnly="true"
msdata:Expression="Parent(Relation1).SpecName" type="xs:string"
msdata:targetNamespace="" minOccurs="0" />

Using Reflector, I found that the Clone method calls DataTable.Reset, which
calls DataTable.Reset.  The Reset method removes all of the ParentRelations:


collection1 = this.ParentRelations;
num1 = collection1.Count;
while ((num1 > 0))
{
 num1 = (num1 - 1);
collection1.RemoveAt(num1);
}


The expression itself is still copied from the source table to the clone.
As soon as an expression is set on a column, it is evaluated.  The cloned
table does not have any parent relations, so the NullReferenceException is
generated.

Kirk Allen Evans
www.xmlandasp.net
Web Log: http://blogs.xmladvice.com/kaevans

-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Gary Davidson
Sent: Friday, January 23, 2004 7:04 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] DataTable.Clone and NullReferenceException

Try this

TypeDataTable dt = dv.Table as TypeDataTable;
DataSet ds = new DataSet();
ds.Merge(dt, true,MissingSchemaAction.Add);
return ds;






Gary Davidson





>From: Kirk Allen Evans <[EMAIL PROTECTED]>
>Reply-To: "Moderated discussion of advanced .NET topics."
><[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: [ADVANCED-DOTNET] DataTable.Clone and NullReferenceException
>Date: Thu, 22 Jan 2004 18:30:25 -0500
>
>I am using an API that exposes a DataView of a typed DataTable, I have no
>control over this API.  The DataTable that I am trying to use already
>belongs to a typed DataSet.  I need to import the DataTable to another
>DataSet to be able to work with it.
>
>Tried using ds.Merge(table).  Internally, .Merge calls the .Clone method on
>the table being merged in.  For some reason, .Clone throws a
>NullReferenceException when .Clone is called.  I verified that the table
>has
>both rows and columns, and the code decompile for System.Data.DataTable in
>Reflector didn't show anything obvious that would cause the exception.
>
>//Calling code
>private DataSet GetDS(DataView dv)
>{
>     DataTable dt = dv.Table;
>     DataSet ds = new DataSet();
>     ds.Merge(dt);
>     return ds;
>}
>
>
>'Pseudo class
>Public Class fooTable
>     Inherits DataTable
>
>     Public Overrides Function ToString() As String
>         Return Me.TableName & "foo"
>     End Function
>
>     Public Overrides Function Clone() As DataTable
>         'Following line throws the exception
>         Dim dt As DataTable = MyBase.Clone()
>
>         Dim cln As fooTable = CType(dt, fooTable)
>         cln.TableName = Me.ToString()
>         Return cln
>     End Function
>End Class
>
>
>So, has anyone see a case where an inherited DataTable could throw a
>NullReferenceException in the .Clone method?
>
>===================================
>This list is hosted by DevelopMentorR  http://www.develop.com
>Some .NET courses you may be interested in:
>
>NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
>http://www.develop.com/courses/gaspdotnetls
>
>View archives and manage your subscription(s) at http://discuss.develop.com

_________________________________________________________________
Check out the coupons and bargains on MSN Offers!
http://shopping.msn.com/softcontent/softcontent.aspx?scmId=1418

===================================
This list is hosted by DevelopMentorR  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to