Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

777 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