Link to home
Start Free TrialLog in
Avatar of hjgode
hjgodeFlag for Germany

asked on

Compact Framework c#: Dataset DataRow.get fails during export

I am doing a small application which handles data via a dataset. I can save the dataset to a xml file without any problem using dsetDB.WriteXml (strXMLFile). But I also need an export to a tab delimited file.

The problem is, that I always get an exception ArgumenException at about 1024bytes are read!
I have tried using string instead of stringbuilder, I have tried different versions of streams. No success.

Interestingly, the progressbar always runs up to the end of all rows (means: it looks like all rows are read), but when the exception occurs, the i counter says 34.
The file always ends up with under 1024 bytes written.

I tried many, many different approachs, ie I wrote line by line to the file, field by field, build all into one stringbuilder string and then write: no chance. The code always breaks with the same no leading exception.

The full source is available at www.hjgode.de/dotnet/InventurNet1.zip
You must undef Intermec, to run in emulation. The xml file should be placed into the same dir as the executable, as the app will only look there for the xml file to read.

The code lines are:

private void ExportText(string strFile)
{
      string strTemp = "";
      System.Text.StringBuilder strLine = new System.Text.StringBuilder();
      string delimStr = "\t";
      char [] delimiter = delimStr.ToCharArray();

      //StreamWriter srdrFile = File.CreateText (strFile);
      StreamWriter srdrFile = new StreamWriter(strFile,false, System.Text.Encoding.Default ,64);
      srdrFile.AutoFlush = true;

      //(new FileStream(strFile, FileMode.Create , FileAccess.ReadWrite ));
      //UnbindControls();
      //DisableControls();
      Cursor.Current = Cursors.WaitCursor ;
      Application.DoEvents();
      FileInfo fi=new FileInfo(strFile);
      long fileLen=fi.Length;
      pb.Maximum=dtabInventur.Rows.Count ;
      pb.Minimum=0;
      pb.Visible=true;
      dtabInventur.DefaultView.RowFilter=String.Empty ;
      DataRow drowInventur;
      try
      {
            for (int i=0; i< dtabInventur.Rows.Count-1; i ++)
            {
                  drowInventur = dtabInventur.Rows[i] ;

                  strTemp="";
                  strTemp =  drowInventur[0].ToString() + delimStr;
                  strTemp += drowInventur[1].ToString() + delimStr;
                  strTemp += drowInventur[2].ToString() + delimStr;
                  strTemp += drowInventur[3].ToString() + delimStr;
                  strTemp += drowInventur[4].ToString() + delimStr;
                  strTemp += drowInventur[5].ToString() + delimStr;
                  strTemp += drowInventur[6].ToString() ;

                  srdrFile.WriteLine(strTemp);
                  pb.Value += i ;
                  srdrFile.Flush ();

                  Application.DoEvents();
            }
      }
      catch (Exception wf)
      {
            MessageBox.Show(wf.Message + "\n" + wf.ToString(), "Uuuups");
      }
      finally
      {

            srdrFile.Flush();
            Application.DoEvents();
            pb.Visible=false;
            Cursor.Current=Cursors.Default ;

            srdrFile.Close ();
            //BindControls();
            //EnableControls();

      }
}
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hjgode

ASKER

OK, I see the problems with the lines you mention (looks like late night programming). Thanks for the hints.

The ArgumentException breaks and the debugger sets the focus on i++ in the for line.

There are no nulls. It is true, that the most fields are empty strings, but I also tested with reading always the same row and it will fail with the same exception. As it everytime near 1024? bytes are written, it looked like a buffer problem. I increased the stream buffer, used AutoFlush and tried Flush(). No change affected the Exception.

I would like you to get the whole code and check what you guess, I did not find any solution. I also find a similar problem mentioned in hackerboard.de (an german programmer board) raised by another guy getting the same error.
Avatar of hjgode

ASKER

The tip for pb.value was the source of the error!

I always wondered, why the progressbar was near at the end, but the i was only at 34 (of 850 rows).

I changed it to pb.value = i;

Thanks a lot and enjoy your points.