Hi All

I hope someone can help as I am stumped.

Usuing Visual Studio 2008, C#, Dot Net Level 3.5 to create a web page
application.

The following is in a button.click event.

The task I have been given is to retrieve a dataset from a database (a
payment list).
This dataset is of indeterminate size (ie it will contain anything
from 1 to hundreds of
records). I have to take the data and reformat the records into a
seperate text file,
each text file containing no more than 50 records. So, in essence, if
I have a dataset
of 125 records I will end up with 3 text files, 2 containing 50
records and 1 containing
25 records. These files are to be stored on the user's desktop for
further processing.

After the every 50 records I am trying to save a file to the desktop.
I want to assign
a different name to the file each time

ie

filename-1.txt  (containing maximum 50 records)
filename-2.txt  (containing maximum 50 records)
filename-3.txt  (containing the residue)

etc

All is going fine apart from 1 problem. (See my code at the foot of
this message)

If I include the line of code

              cOne.Response.End(); /// ******** 1

then I get the opportunity to save the first file and the script
immediately ceases executing.

I can start the process again by pressing the button again but the
same occurs, I only
get the first file.

If I remove the line of code

              cOne.Response.End(); /// ******** 1

I get a single file with only the last 25 records included. The first
100 (in 2 files are lost).


I would appreciate any help I can get as I am completely stumped. I
have searched the
web but not come up with answers to my problems.

Thanks in advance for any help offered.

Regards

Iain



        ---------------------------------------------
        My Code
        ---------------------------------------------


        OleDbConnection con
= ..........................................................
        OleDbCommand cmd = new OleDbCommand();

        string LSqlString = ................. get the data to create
the file

        con.Open();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = LSqlString;
        OleDbDataReader dsPaymentLines = cmd.ExecuteReader();

        DateTime todaysDate = DateTime.Now;
        string   format     = "dd-MM-yyyy";
        string   newFormat  = todaysDate.ToString(format);

        string fileName = "";
        int extention = 1;
        string fileString = "";

        if (dsPaymentLines.HasRows)
          {
          // We are now going to create the payment file
          // First create the header
          fileString = createTheHeader();
          int recordCount = 0;
          while (dsPaymentLines.Read())
            {
            // We can only assign 50 records to a file so if we get to
50 then save the file as -1, -2 etc
            recordCount = recordCount + 1;
            printFile       = true;

            fileString =      ........ build the output string

            if (recordCount == 50)
              {
              printFile = false;
              fileString = fileString + createTheTail();
              fileName = "Payment_List_" + extention.ToString() +
".txt";
              System.Web.HttpContext cOne =
System.Web.HttpContext.Current;
              cOne.Response.Clear();
              cOne.Response.ClearHeaders();
              cOne.Response.AddHeader("content-disposition",
"attachment; filename=" + fileName);
              cOne.Response.Charset = "";
              cOne.Response.Buffer = true;
 
cOne.Response.Cache.SetCacheability(HttpCacheability.NoCache);
              cOne.Response.ContentType = "application/vnd.text";
              System.IO.StringWriter stringWritec = new
System.IO.StringWriter();
              System.Web.UI.HtmlTextWriter htmlWritec = new
HtmlTextWriter(stringWritec);
              cOne.Response.Write(fileString);
//              cOne.Response.End();                /// ******** 1

              extention = extention + 1;
              fileString = createTheHeader();
              recordCount = 0;  // reset the record count

              fileString = createTheHeader();

              }
            }
          fileString = fileString + createTheTail();

          // This will deal with the overflow of records
          // ie the was not enough to make a full file of 50
          // so create a smaller file
          if (printFile)
            {
            fileString = fileString + createTheTail();
            fileName = "Payment_List_" + extention.ToString() +
".txt";
            System.Web.HttpContext cOne =
System.Web.HttpContext.Current;
            cOne.Response.Clear();
            cOne.Response.ClearHeaders();
            cOne.Response.AddHeader("content-disposition",
"attachment; filename=" + fileName);
            cOne.Response.Charset = "";
            cOne.Response.Buffer = true;
 
cOne.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            cOne.Response.ContentType = "application/vnd.text";
            System.IO.StringWriter stringWritec = new
System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWritec = new
HtmlTextWriter(stringWritec);
            cOne.Response.Write(fileString);
            cOne.Response.End();                /// ******** 2
            }

          }


        con.Close();

Reply via email to