Problem Casting from Dataset to Strongly Typed Dataset

Problem Casting from Dataset to Strongly Typed Dataset

Post by Jason Hil » Thu, 10 Oct 2002 17:24:05



I am using the MS Data Access Application Blocks helper classes to execute
SQL statements against a SQL Server 2000 database and return datasets. I
have created a strongly typed dataset from an XSD file, which obviously
inherits the System.Data.Dataset class.

I am then trying to cast the System.Data.Dataset return type from the
SqlHelper.ExecuteDataset method to my strongly typed dataset but get a
"specified cast is not valid" error.

Can anyone please help with this problem?

Thanks,

Jason

 
 
 

Problem Casting from Dataset to Strongly Typed Dataset

Post by Malcolm Stewa » Fri, 11 Oct 2002 02:31:45


Jason,

<<
I am using the MS Data Access Application Blocks helper classes to execute
SQL statements against a SQL Server 2000 database and return datasets. I
have created a strongly typed dataset from an XSD file, which obviously
inherits the System.Data.Dataset class.

I am then trying to cast the System.Data.Dataset return type from the
SqlHelper.ExecuteDataset method to my strongly typed dataset but get a
"specified cast is not valid" error.

Can anyone please help with this problem?

This is a limitation of object oriented design. You can't cast a base class
to a derived class unless the base class was created by casting the derived
class.

e.g. assume class A is a base class and class B inherits from it. Then:

Dim X As A = New A(): Dim Y As B = A   ' this will fail - not allowed
Dim X As A = New B(): Dim Y As B = A   ' this will work since was
originally type B

To work around the problem, you will have to change the DAAB to use your
typed DataSet class instead of the generic DataSet class.

Malcolm Stewart
Microsoft Developer Support
This posting is provided "AS IS", with no warranties, and confers no rights.

Are you secure?  For information about the Strategic Technology Protection
Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

 
 
 

Problem Casting from Dataset to Strongly Typed Dataset

Post by Jaso » Fri, 11 Oct 2002 13:08:08


Thanks for your response Malcolm. I guess it really is a limitation of *my
understanding* of object oriented design!!!

However, changing the DAAB to return a specific typed dataset is not going
to work for me because I will have many different typed datasets so cannot
just choose one as the return type from the DAAB method. I wanted a generic
method of populating one of these typed datasets by calling the DAAB helper
methods, although it sounds like this will not be possible then??

Thanks,

Jason


Quote:> Jason,

> <<
> I am using the MS Data Access Application Blocks helper classes to execute
> SQL statements against a SQL Server 2000 database and return datasets. I
> have created a strongly typed dataset from an XSD file, which obviously
> inherits the System.Data.Dataset class.

> I am then trying to cast the System.Data.Dataset return type from the
> SqlHelper.ExecuteDataset method to my strongly typed dataset but get a
> "specified cast is not valid" error.

> Can anyone please help with this problem?

> This is a limitation of object oriented design. You can't cast a base
class
> to a derived class unless the base class was created by casting the
derived
> class.

> e.g. assume class A is a base class and class B inherits from it. Then:

> Dim X As A = New A(): Dim Y As B = A   ' this will fail - not allowed
> Dim X As A = New B(): Dim Y As B = A   ' this will work since was
> originally type B

> To work around the problem, you will have to change the DAAB to use your
> typed DataSet class instead of the generic DataSet class.

> Malcolm Stewart
> Microsoft Developer Support
> This posting is provided "AS IS", with no warranties, and confers no
rights.

> Are you secure?  For information about the Strategic Technology Protection
> Program and to order your FREE Security Tool Kit, please visit
> http://www.microsoft.com/security.

 
 
 

Problem Casting from Dataset to Strongly Typed Dataset

Post by Malcolm Stewa » Sat, 12 Oct 2002 00:48:59


Jason,

<<
Thanks for your response Malcolm. I guess it really is a limitation of *my
understanding* of object oriented design!!!

However, changing the DAAB to return a specific typed dataset is not going
to work for me because I will have many different typed datasets so cannot
just choose one as the return type from the DAAB method. I wanted a generic
method of populating one of these typed datasets by calling the DAAB helper
methods, although it sounds like this will not be possible then??

You could change the DAAB methods to accept an additional parameter that
contains a newly created DataSet of the type you want and fill that instead
of creating a DataSet from scratch. So, small change to DAAB, and some
extra initialization code in the calling procedure.

Malcolm Stewart
Microsoft Developer Support
This posting is provided "AS IS", with no warranties, and confers no rights.

Are you secure?  For information about the Strategic Technology Protection
Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

 
 
 

Problem Casting from Dataset to Strongly Typed Dataset

Post by Jaso » Sat, 12 Oct 2002 08:04:41


Malcolm,

That's exactly what I ended up doing last night after giving it some more
thought!!

I now pass in my typed dataset into the method, with the parameter being
defined as type Object. I also pass in the name of the DataTable to populate
as I may have multiple DataTables in the dataset and didn't want the DAAB
creating a new default DataTable. This works like a charm now!

Thanks for your help.

Jason


Quote:> Jason,

> <<
> Thanks for your response Malcolm. I guess it really is a limitation of *my
> understanding* of object oriented design!!!

> However, changing the DAAB to return a specific typed dataset is not going
> to work for me because I will have many different typed datasets so cannot
> just choose one as the return type from the DAAB method. I wanted a
generic
> method of populating one of these typed datasets by calling the DAAB
helper
> methods, although it sounds like this will not be possible then??

> You could change the DAAB methods to accept an additional parameter that
> contains a newly created DataSet of the type you want and fill that
instead
> of creating a DataSet from scratch. So, small change to DAAB, and some
> extra initialization code in the calling procedure.

> Malcolm Stewart
> Microsoft Developer Support
> This posting is provided "AS IS", with no warranties, and confers no
rights.

> Are you secure?  For information about the Strategic Technology Protection
> Program and to order your FREE Security Tool Kit, please visit
> http://www.microsoft.com/security.