Strongly typed Datasets and explicit cast..

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
 
N

Nicholas Paldino [.NET/C# MVP]

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

Roman S. Golubin

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.
 

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