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 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

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

===================================
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