468,290 Members | 2,118 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

" 'StackOverflowExcepton' was unhandled " C# Windows App

joedeene
583 512MB
Hello there, I am having a problem and it is frustrating me because I've been trying to figure it out, and I've even modified the code a few times but the same exception occurs: " 'StackOverflowExcepton' was unhandled "

Project Details:

Ok, I am creating a text file that will store database information of some customers of mine and I'm using the StreamReader and StreamWriter Class. I can successfully get the information and customer data written to the file, but when I try to retrieve it and store the retrieved data into the listbox I get that exception. I am using a class library/.DLL also in the project, which shouldn't matter because I can call the AddCustomer() function properly. Here's some of the code...

Expand|Select|Wrap|Line Numbers
  1.  public string[] retrieveCustomers()
  2.         {
  3.             System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on
  4.  
  5.             int i = 0;
  6.             string newLine;
  7.             while ((newLine = CustomerStreamReader.ReadLine()) != null)
  8.             {
  9.                 retrieveCustomers().SetValue(newLine, i);
  10.                 i++;
  11.             }
  12.  
  13.             CustomerStreamReader.Close();
  14.  
  15.             return retrieveCustomers();
  16.         }
And I call it from the form's button like this;

Expand|Select|Wrap|Line Numbers
  1.  private void btnReload_Click(object sender, EventArgs e)
  2.         {
  3.             listBoxCustomers.Items.Clear();
  4.             string[] CustomersCollection = papers.retrieveCustomers(); //papers is my .DLL reference.
  5.             foreach (string customerInformation in CustomersCollection)
  6.             {
  7.                 listBoxCustomers.Items.Add(customerInformation);
  8.             }
  9.         }
But there's no error there, Just like to know why it like freezes, and how to fix it. It says "Make sure there is no infinite loop", I don't think I have one though, and it freezes right at the part where I'm declaring the StreamReader in the .DLL

joedeene
Oct 29 '08 #1
8 1374
tlhintoq
3,525 Expert 2GB
Hello there, I am having a problem and it is frustrating me because I've been trying to figure it out, and I've even modified the code a few times but the same exception occurs: " 'StackOverflowExcepton' was unhandled "

Project Details:

Ok, I am creating a text file that will store database information of some customers of mine and I'm using the StreamReader and StreamWriter Class. I can successfully get the information and customer data written to the file, but when I try to retrieve it and store the retrieved data into the listbox I get that exception. I am using a class library/.DLL also in the project, which shouldn't matter because I can call the AddCustomer() function properly. Here's some of the code...

Expand|Select|Wrap|Line Numbers
  1.  public string[] retrieveCustomers()
  2.         {
  3.             System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on
  4.  
  5.             int i = 0;
  6.             string newLine;
  7.             while ((newLine = CustomerStreamReader.ReadLine()) != null)
  8.             {
  9.                 retrieveCustomers().SetValue(newLine, i);
  10.                 i++;
  11.             }
  12.  
  13.             CustomerStreamReader.Close();
  14.  
  15.             return retrieveCustomers();
  16.         }
And I call it from the form's button like this;

Expand|Select|Wrap|Line Numbers
  1.  private void btnReload_Click(object sender, EventArgs e)
  2.         {
  3.             listBoxCustomers.Items.Clear();
  4.             string[] CustomersCollection = papers.retrieveCustomers(); //papers is my .DLL reference.
  5.             foreach (string customerInformation in CustomersCollection)
  6.             {
  7.                 listBoxCustomers.Items.Add(customerInformation);
  8.             }
  9.         }
But there's no error there, Just like to know why it like freezes, and how to fix it. It says "Make sure there is no infinite loop", I don't think I have one though, and it freezes right at the part where I'm declaring the StreamReader in the .DLL

joedeene
Have you put breakpoint on line 3 to see if it gets called numerous times? Maybe you really do have a loop.

Try wrapping the guts of the function in a Try {} Catch{} statement. That way you can catch the error and read its message.

Try adding a line for console.writeline("Entered this method"); just above the problem line. If your output window gets slammed with messages then you know you have a loop.


Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.
Oct 30 '08 #2
joedeene
583 512MB
Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.
So I can't return the function() after it's value has been changed? It only pauses on the streamreader line, and i'll try that try catch statement and get back to you.

joedeene
Oct 30 '08 #3
r035198x
13,262 8TB
...
Wait wait wait... Line 15 is calling this function. The return loops back to call itself. Yep, its a loop.
Line 15 is never reached. The infinite looping is happening on line 9.
Oct 30 '08 #4
Plater
7,872 Expert 4TB
Line 15 is never reached. The infinite looping is happening on line 9.
Line 9 is also a loop.
this smells of a VB programmer trying to learn c#.

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. public string[] retrieveCustomers() 
  2.     List<string> retval = new List<string>();
  3.     System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on 
  4.  
  5.     string newLine; 
  6.     while ((newLine = CustomerStreamReader.ReadLine()) != null) 
  7.     { 
  8.         retval.Add(newline);
  9.     } 
  10.     CustomerStreamReader.Close(); 
  11.  
  12.     return retval.ToArray();
  13.  
or
Expand|Select|Wrap|Line Numbers
  1. public string[] retrieveCustomers()
  2. {
  3.   List<string> retval = new List<string>();
  4.   System.IO.StreamReader CustomerStreamReader = new System.IO.StreamReader("MyPath"); //This is the line the error is on  
  5.  
  6.   while (!CustomerStreamReader.EndOfStream)
  7.   {
  8.       retval.Add(CustomerStreamReader.ReadLine());
  9.   }
  10.   CustomerStreamReader.Close();
  11.  
  12.   return retval.ToArray();
  13. }
  14.  
  15.  
Oct 30 '08 #5
r035198x
13,262 8TB
..
this smells of a VB programmer trying to learn c#.

...
<Blocks nose because joe is about to walk in />
Oct 30 '08 #6
joedeene
583 512MB
...this smells of a VB programmer trying to learn c#....
Ya, I did Visual Basic.Net for a while and decided to switch, thanks for the help. I'll try it when i get home :)

joedeene
Oct 30 '08 #7
Plater
7,872 Expert 4TB
this smells of a VB programmer trying to learn c#.
I suppose that came off sounding a bit "mean".
What I ment was, in VB you assign the return value of a function, directly TO the name of the function. so if I had a function called foo() that returned a string, inside the function I would set the return value by saying "foo="some string" (or close to that).
How you were ever able to do recursion in VB, I don't know. In regular languages, referencing the name of the function will CALL the function.
Oct 30 '08 #8
joedeene
583 512MB
I suppose that came off sounding a bit "mean".
What I ment was, in VB you assign teh return value of a function, directly TO the name of the function. so if I had a function called foo() that returned a string, inside the function I would set the return value by saying "foo="some string" (or close to that).
How you were ever able to do recursion in VB, I don't know. In regular languages, referencing the name of the function will CALL the function.
Nah, I didn't take it mean, iA said a while back that VB had/teaches lazy habits and I guess that's one of them, but ya I haven't worked with functions much in c# since VB but ya it does make sense that it calls that 'referencing the name of the function will CALL the function'. But it vb you could say something like that, well thanks for the knowledge, I'm home and I'm gonna try the above code now...

Edit: Yay it works, thanks a whole bunch, now I can continue with my database...

joedeene
Oct 30 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.