Converting an untyped datatable into a typed datatable

G

Guest

Hi,

I have a method, which opens a connection, creates an untyped datable, fills it and returns it back.

I have defined a typed datatable in my application as well. For some reasons, I have to call the above mentioned method to get data. Unfortunately, as it returns an untyped datatable, I can't assign my typed datatable variable this value (a base type can't be converted into a sub type automatically).

Without resorting to looping thru the rows and assigning it manually, are there any other procedures so that i could assign the untyped data into my typed datatable? I believe reflection might help.

Thanks in advance!
 
N

Nicholas Paldino [.NET/C# MVP]

Rakesh,

You should be able to save the XML to a stream using the WriteXml
method. Then, create a new instance of the typed data set, and call the
ReadXml method on it, passing the stream that you saved the XML to.

You might want to consider using a temporary file for this, if the cost
of holding all the XML in memory is too high.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rakesh Rajan said:
Hi,

I have a method, which opens a connection, creates an untyped datable, fills it and returns it back.

I have defined a typed datatable in my application as well. For some
reasons, I have to call the above mentioned method to get data.
Unfortunately, as it returns an untyped datatable, I can't assign my typed
datatable variable this value (a base type can't be converted into a sub
type automatically).
Without resorting to looping thru the rows and assigning it manually, are
there any other procedures so that i could assign the untyped data into my
typed datatable? I believe reflection might help.
 
G

Guest

Hi Nicholas,

Thanks a lot!

But aren't there any more 'graceful' methods?


--
Rakesh Rajan


Nicholas Paldino said:
Rakesh,

You should be able to save the XML to a stream using the WriteXml
method. Then, create a new instance of the typed data set, and call the
ReadXml method on it, passing the stream that you saved the XML to.

You might want to consider using a temporary file for this, if the cost
of holding all the XML in memory is too high.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rakesh Rajan said:
Hi,

I have a method, which opens a connection, creates an untyped datable, fills it and returns it back.

I have defined a typed datatable in my application as well. For some
reasons, I have to call the above mentioned method to get data.
Unfortunately, as it returns an untyped datatable, I can't assign my typed
datatable variable this value (a base type can't be converted into a sub
type automatically).
Without resorting to looping thru the rows and assigning it manually, are
there any other procedures so that i could assign the untyped data into my
typed datatable? I believe reflection might help.
Thanks in advance!
 
N

Nicholas Paldino [.NET/C# MVP]

Rakesh,

That IS the graceful method. Since strongly typed data sets derive from
DataSet, and the strong-typedness (if that is a word) comes from added
methods and properties to that data set, it would make sense to use the
methods that already exist on the DataSet to populate it. You could create
a constructor on your strongly typed data set that takes a stream, or a
DataSet and performs this operation for you. That's about as easy as it
could get.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rakesh Rajan said:
Hi Nicholas,

Thanks a lot!

But aren't there any more 'graceful' methods?


--
Rakesh Rajan


Nicholas Paldino said:
Rakesh,

You should be able to save the XML to a stream using the WriteXml
method. Then, create a new instance of the typed data set, and call the
ReadXml method on it, passing the stream that you saved the XML to.

You might want to consider using a temporary file for this, if the cost
of holding all the XML in memory is too high.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rakesh Rajan said:
Hi,

I have a method, which opens a connection, creates an untyped datable, fills it and returns it back.

I have defined a typed datatable in my application as well. For some
reasons, I have to call the above mentioned method to get data.
Unfortunately, as it returns an untyped datatable, I can't assign my typed
datatable variable this value (a base type can't be converted into a sub
type automatically).
Without resorting to looping thru the rows and assigning it manually,
are
there any other procedures so that i could assign the untyped data into my
typed datatable? I believe reflection might help.
Thanks in advance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top