Solved

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

Posted on 2004-08-05
3
1,574 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now