MemoryStream instead of StreamWriter Problem  
Author Message
ashk1860





PostPosted: Visual C# General, MemoryStream instead of StreamWriter Problem Top

I have a method which make a file with StreamWriter and then save it to database. I want to make a new method base on this method but using MemoryStream instead of StreamWriter.

1- When I give the byte[] in constructor and then store it in database, everything is OK but When I try to use Write Method of memoryStream nothing happend!!! it means that just an empty file will store to the database!)

this means that it works OK with the folowing line:

MemoryStream memStream = new MemoryStream(byteArray);

but if I use the folowing lines instead, it doesn't work.

MemoryStream memStream = new MemoryStream();

memStream.Write (secondString,0,secondString .Length );

2-I have used Write and WriteLine methods in previous procedure, but MemoryStream don't have WriteLine method so I dont know how to solve this problem even if my first problem get solve.



Visual C#20  
 
 
ahmedilyas





PostPosted: Visual C# General, MemoryStream instead of StreamWriter Problem Top

could I ask, how have you checked to see if the memorystream contains data remember set the position to 0 to start reading it from the beginning. you could use GetBuffer() which will return you the bytes written to the stream

the way to write data TO the mem stream is as what you are doing. Also be sure that secondString does contain data. I have use this many times and just checked, it works fine :-)



 
 
ashk1860





PostPosted: Visual C# General, MemoryStream instead of StreamWriter Problem Top

hi

thank you for your response

secondString's value comes from the following line:

byte[] secondString = System.Text.Encoding.UTF8.GetBytes("Hello");

so obviosly it is not empty


 
 
ashk1860





PostPosted: Visual C# General, MemoryStream instead of StreamWriter Problem Top

I have find out the problem. when I wrote bytes to mamory stream the position would be in the end of stream. so there is no more bytes to read when the pointer in the end of the stream. so using

memStream.Position=0;

have solved the problem