Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

837 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