Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implicit conversion to System.IO.MemoryStream

Status
Not open for further replies.

cathiec

Programmer
Oct 21, 2003
139
IE
I am using a memory stream object and i want to use the create stream method to display a dataset in the browser window. I get the error:

"Cannot implicitly convert type 'System.IO.Stream' to 'System.IO.MemoryStream'"

on the line:

s = SF.CreateStream(GetData());

s is the memory stream object

SF is an object of the eXport.NET class (an add on that we use to export excel documents to browser - it supports the create stream method)

GetData() is the method that returns the dataset

private DataSet GetData()
{
SqlDataAdapter da;
DataSet ds;
ds = new DataSet();
da = new SqlDataAdapter("stpDEBgetInsuranceMailMerge", strConnection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.Fill(ds);
return(ds);
}

i would greatly appreciate any suggestions,

 
cathiec,
The reason why you're getting the error is that SF.CreateStream() returns an object of type System.IO.Stream and you're trying to assign such object to the variable s, which in turn is of type System.IO.MemoryStream. You're not allowed to do that in C# because that would be the same as assigning, for example, a string to an integer.

You can do one of two things:

1 - Try to see if SF has a method that returns a MemoryStream (not just as Stream) and use that method instead, or

2 - Cast the returned Stream object into a MemoryStream object as in:

s = (System.IO.MemoryStream)SF.CreateStream(GetData());

I'm not 100% certain that you can convert straight from Stream to MemoryStream, but I don't see any reason why you wouldn't be able to.

Hope this helps!

JC





Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
I think the SF.CreateStream() returns a generic System.IO.Stream object and you should determine its type and do the unboxing accordingly. It could be exactly what you want e.g. System.IO.MemoryStream or another derived from System.IO.Stream:
Code:
s = SF.CreateStream(GetData()); 
System.Type type = s.GetType();

if (type.ToString() =="System.IO.MemoryStream" )
{
   // It is a MemoryStream
   // Process here 
}
if (type.ToString() =="System.Data.OracleClient.OracleBFile")
{
   // Process OracleBFile here 
}
//...
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top