cast dataset to typed dataset

  • Thread starter Thread starter Richard Roche
  • Start date Start date
R

Richard Roche

my data layer returns vanilla dataset and i want to cast
it to a typed dataset, but keep getting:

Specified Cast is not Valid

I've dropped a dataset onto my asp.net form
generated a new dataset and added to the form.

dim ds as dataset
dim dsT as dsTyped

ds = oData.Getdata
dsT = ctype(ds, dsT) ' this fails w/ Specified Cast is
not Valid

i'm fairly certain the generic dataset should match the
typed.

any advice or where to read is appreciated.
 
Thanks for the prompt helpful response.

I was assuming there was a way to promote a generic ds to
a typed ds (assuming the schema is aligned), but since
that's not true unless it was orginally created as an
instance of that 'type' i'll work around.

I appreciate the help.
-----Original Message-----
This is from ADO.Net by David Sceppa and probably is the answer to your question...

"However, you can 'promote' an untyped DataSet to a
strongly typed DataSet class * ONLY IF * the untyped
DataSet was originally created as an instance of that same
strongly
typed DataSet class. The following code snippet should help clarify this behavior:

Visual Basic .NET
Dim dsStrong1, dsStrong2 As Chapter9
Dim dsUntyped As DataSet

'This code will succeed.
dsStrong1 = New Chapter9()
dsUntyped = CType(dsStrong1, DataSet)
dsStrong2 = CType(dsUntyped, Chapter9)

'This code will throw an exception.
dsUntyped = New DataSet()
dsStrong2 = CType(dsUntyped, Chapter9)


Notice the 'Only If' in the sentence above... Was your
dataset created from the same Strongly typed dataset? If
not then what you are trying to do will not work...
Want to know more? Check out the MSDN Library at
http://msdn.microsoft.com or the Microsoft Knowledge Base
at http://support.microsoft.com
 
Richard,

Though you won't be able to simply cast the untyped DataSet
to a typed DataSet, you can use the DataSet's Merge method to
import the data. The following code should work for you:

Dim ds As DataSet
Dim dsT As dsTyped

ds = oData.Getdata
dsT = New dsTyped()
dsT.Merge(ds)

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
Back
Top