Reading the data from a Memory Stream

I built code into a memory stream. I was using the memory stream to output a file using a dialogbox and this worked great.. I would like to continue to use the memory stream, but now I am trying to change the logic to either throw it into a temp directory with in the IIS site itself, or (preferably) to move the data from the memory stream into a SQL table..

Below is the code that uses the memorystream (mstream)  to push to a dialog save box. and save to a flat file once the user choose where to put it..

I don't need the connection or execute of the SQL part.. But I am unsure how to read the memory stream record for record and update the data to a SqL Table.. The Read/insert section..

Or if it would need to be moving it to a flat file without the dialog, again, I only know how to accomplish that with a read/write... I though am unsure if the memory stream with convert the data line item, by line item back out again.

Or with either how you say here are all the records.. Whoosh.. Break them into seprate lines or records..
private void SaveDialog2(string BaseFileName,System.IO.MemoryStream mstream)
        {
            // the input was saved to a temp area on IIS c:\ drive (or if test run on programers pc)
            // Now you need to pick up that file and set it to a savedialog so the user can
            // save the file where they want to..
            //Set the appropriate ContentType.

            //OutputPath = @"\\Fs01\Shared\Information Techology\LOG FILES\AdPower\Import\Account\Faimul11" + strDate + ".dat";
            //OutputPath = @"C:\TempForIIS01\Faimul11" + strDate + ".dat";
            try
            {
                //Convert the memorystream to an array of bytes.
                byte[] byteArray = mstream.ToArray();
                //Clean up the memory stream
                mstream.Flush();
                mstream.Close();
                // Clear all content output from the buffer stream
                Response.Clear();
                // Add a HTTP header to the output stream that specifies the default filename
                // for the browser's download dialog
                string attachmentfilename = "attachment; filename=" + BaseFileName + ".dat";
                Response.AddHeader("Content-Disposition", attachmentfilename);
                //// Add a HTTP header to the output stream that contains the 
                //// content length(File Size). This lets the browser know how much data is being transfered
                string byteLen = byteArray.Length.ToString();
                //Response.AddHeader("Content-Length", byteArray.Length.ToString());
                // Set the HTTP MIME type of the output stream
                Response.ContentType = "application/octet-stream";
                // Write the data out to the client.
                Response.BinaryWrite(byteArray);


            }
            catch (Exception)
            {
                throw;
            }
        }

Open in new window

LVL 1
moosetrackerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Use a StreamReader on the memory string to get the rows.

StreamReader sr;
sr = new StreamReader(mstream);

while (!sr.EOF)  // while not end of file
{
  // do something!
}

moosetrackerAuthor Commented:
I got the code almost done, you can see below, if you see any errors..

But I got an error with the    while (!sr.EOF)

System.IO.StreamReader' does not contain a definition for 'EOF' and no extension method 'EOF' accepting a first argument of type 'System.IO.StreamReader' could be found (are you missing a using directive or an assembly reference?)
private void SaveSQLWrite(string BaseFileName, System.IO.MemoryStream mstream)
        {
            try
            {

                string SQLStatement = string.Empty;
                string ConnectionString = connectionInfo;
                    cnnct = new SqlConnection(ConnectionString);
                    cnnct.Open();
                StreamReader sr;
                sr = new StreamReader(mstream);

                while (!sr.EOF)  // while not end of file
                {
                    string SQLStatementStart = "Insert into  VS.tmpBadPhone " +
                                          "Select '" + sr + "'";
                    // Process
                    cmd = new SqlCommand(SQLStatement);
                    cmd.Connection = cnnct;
                     cmd.ExecuteNonQuery();
                     cmd = null;
               }

            }
            catch (Exception err)
            {
                throw new Exception("Error Writing to tmpBadPhone:  " + err.Message);
                throw err;
            }
            finally
            {
                if (cnnct.State == ConnectionState.Open)
                    cnnct.Close();
            }

Open in new window

Kyle AbrahamsSenior .Net DeveloperCommented:
should be ! sr.EndOfStream . . . didn't have my compiler open.

the loop looks good . . . not you may have to open the connection each time (forget if it throws an error or not).

you can also combine these two lines:
 cmd = new SqlCommand(SQLStatement);
                    cmd.Connection = cnnct;

cmd = new SqlCommand(SQLStatement, cnnct);

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

moosetrackerAuthor Commented:
Thanks. I had found something else on the web.

while ((srLine = sr.ReadLine()) != null)  

But like your logic better.. I figured though that the sr.ReadLine() is the correct set for reading each line, so I using that instead of sr. Strange last night there was no intellisense for the sr field, this moring it was there..

Now with running it, I get a new error, before we even get to the read logic.. It is pointing to the mstream not being set up right. I am confused by this because the building of mstream was not altered. And it works fine when passing it to the Save dialog box..

Right at the line..

Sr = new StreamReadre(mstream);

I get the error.  “Stream was not readable”..

Is there some extra logic I have to set up while building the stream to make it readable?

Below is the original code to build the memory stream that is working pushing it to the dialog box..
//Initialize at higher level..
MemoryStream ReturnStream = new MemoryStream();
StreamWriter sw;
:
:

      //Create a streamwriter to write to the memory stream
      sw = new StreamWriter(ReturnStream);

                //Process Stored Procedure Query..
                cmd.Connection = cnnct;
                reader = cmd.ExecuteReader();

                //Process each Record in Reader and move into the text table
                while (reader.Read())
                    ProcessEachRecord(reader);

:
:

       private void ProcessEachRecord(SqlDataReader RecordInfo)
        {
:
:
            if (Phone.Trim().Length >= 10 && Int64.TryParse(strphone, out result))
            {
                txtLine = "[cit]";
                sw.WriteLine(txtLine);

                if (AddUpdDelFlag == "D")
                    txtLine = "operation=Remove";
                else if (AddUpdDelFlag == "A" || AddUpdDelFlag == "U")
                    txtLine = "operation=Insert";
                sw.WriteLine(txtLine);


                txtLine = "item_type=phone";
                sw.WriteLine(txtLine);
                txtLine = "credit_status=B";
                sw.WriteLine(txtLine);
                txtLine = "range_begin=" + Phone;
                sw.WriteLine(txtLine);
                txtLine = "credit_notes=" + Credit_Descript;
                sw.WriteLine(txtLine);
            }
        }

Open in new window

Kyle AbrahamsSenior .Net DeveloperCommented:
you never close the stream writer  . . . and I wouldn't call WriteLine . . . I would just writie it so you have one record per line.

I would recommend you first implement both pieces of code locally, writing to an actual text file so you can see what's going on.

Then when you're done with that, use a memory stream instead.

(Simply just point your streamwriter & reader at a local file . . . you can point back to a memory stream when needed)

Also what happens if there is no data?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperCommented:
Good article that shows proper way to read and write from files.

I prefer to not do inline assignments in my if statements, can get confusing, hence the
!sr.EndOfStream  . . . but the assingment is fine if you're used to it.

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132
moosetrackerAuthor Commented:
I felt the same about the inline code, I knew I wouldn't know what I was doing in this code when I returned to it months later, without comments around it. So I changed it to your logic..

Surprisingly, I did change the code to write to a text file just before leaving work today, I changed the memoryStream to point to a file on the C drive..  That worked just fine..

So I know all my code is now working without the Memory Stream..  I could leave it like this, as it is working and doing what I want.. We have a folder that automatically deletes these type of temp file daily, just for this purpose..

The textWriter & reader I had used other times, the stream reader/writer is a little new to me, and the memorystream is new to me also..

I just hate to learn something new like the Memory Stream, then yank out all the code.

I know 6 months from now I will want it again, and have no working code to go back to..  Darn.. Darn.. Double Darn...

Thanks for your help though.. The main thing is that it is working..
Kyle AbrahamsSenior .Net DeveloperCommented:
A stream is nothing more than the thing that is between the file and your program.  It is the link between the two things.

Writing to a text file, memory stream, file stream should be no different in terms of your logic, and if you want help I can give you some pointers.

A Memory stream is a bit better because it works in memory (as the name applies) versus on a file (which can be opened or locked).  Is only one person using your program at any given time or do you have random file names, etc?

moosetrackerAuthor Commented:
Thanks ged325.. I would like to get the memory stream working.  Even though the file stream is working. I do realize the benefit of the memoree stream, and so see where maybe I can get around it with the file stream, but this would be nice to learn..

I just can't figure out why, the memory stream will work going to the dialog box. Yet the streamReader will work with a file stream but not the Memory stream.. Seems like a disconnect of two working items that come together and go ka-blu-ee...

Is there anything from my code that you would like to review again, that can help you see what I did wrong? Since the code is split up between code on a form, and code in a class, it's a little bit spread out.
Kyle AbrahamsSenior .Net DeveloperCommented:
Would recommend you open another question or you can ask for this one to be opened again (I don't mind) . . . but "answered" questions are harder to track (for response purposes).


if you're only using the SW in ProcessEachRecord, don't initialize to you need it.  At the very least I would recommend passing both the stream and the writer by reference:
eg:
 ProcessEachRecord(reader, &sw, &ReturnStream);


Lastly, where are you reading from it? (Looks like a different error)


If not I would recommend first trying a simple application with a button (eg: come back to the sql later)
Just try to create the memory stream, write to it, and then read from it.






It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.