Solved

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

Posted on 2004-08-05
3
1,569 Views
Last Modified: 2010-08-05
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();

      }
}
0
Comment
Question by:hjgode
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11731967
well the first thing i see here is ....

pb.Value += i ;

it should be pb.Value++;

also

for (int i=0; i< dtabInventur.Rows.Count-1; i ++)

should be

for (int i=0; i< dtabInventur.Rows.Count; i ++)

or easier

foreach(DataRow dr in dtabInventur.Rows)


which line do you get the ArgumentException on ? Out of curiosity do you have nulls in your datatable ?


0
 
LVL 16

Author Comment

by:hjgode
ID: 11733897
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.
0
 
LVL 16

Author Comment

by:hjgode
ID: 11734125
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now