Strongly typed Datasets and explicit cast..

  • Thread starter Thread starter Oscar Thornell
  • Start date Start date
O

Oscar Thornell

Hi,

I have a general all purpose class that can take the name of a stored
procedure and return a dataset...

This general dataset that is returned I wish to cast to a strongly typed
dataset of my choosing.
I have the XML Schema and a generated specialization of the DataSet that I
wan´t to cast to...

Generated by xsd.exe
public class MyStronglyTypedDS : DataSet {
....
}

My app code: (wich causes Invalid Cast Exception...)

MyStronglyTypedDS ds = (MyStronglyTypedDS) obj.GetDataSet("MyStoredProc");


My DAL code:

public DataSet GetDataSet(string sproc) {
....
return ds;
}

I would rely appreciate some feedback!
Thanks

/Oscar
 
Oscar,

You can not do this. What you have to do is get the contents of the
dataset into some other medium (string, stream, file, etc, etc), and then
create a new instance of your typed dataset. Once you have that, you should
be able to load the contents of the other dataset in that medium (using
ReadXml, most likely), and then it will populate the new typed dataset
appropriately.

Hope this helps.
 
Hi, Oscar Thornell
I have a general all purpose class that can take the name of a stored
procedure and return a dataset...

This general dataset that is returned I wish to cast to a strongly typed
dataset of my choosing.
I have the XML Schema and a generated specialization of the DataSet that I
wan´t to cast to...

Generated by xsd.exe
public class MyStronglyTypedDS : DataSet {
...
}

My app code: (wich causes Invalid Cast Exception...)

MyStronglyTypedDS ds = (MyStronglyTypedDS) obj.GetDataSet("MyStoredProc");

Try

MyStronglyTypedDS ds = new MyStronglyTypedDS();
ds.Merge(obj.GetDataSet("MyStoredProc"));

Hope this helps.
 
Back
Top