Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting stack over flow exception when running below peace of code at Lista.add().
I am not able to identify the cause.

C#
if (dt.Rows.Count > 0)
               {
                   lista.Clear();
                   for (int i = 0; i < dt.Rows.Count; i++)
                   {
                           lista.Add(new Inventory(dt.Rows[i]["ID"].ToString(),dt.Rows[i]["Serial_No"].ToString(), dt.Rows[i]["Part_Code"].ToString(), dt.Rows[i]["Date"].ToString(), dt.Rows[i]["Quantity"].ToString(), dt.Rows[i]["Category"].ToString()));
                   }


help or suggesstion would be appreciated

Thanks
Posted
Comments
vijay__p 14-Jun-13 0:22am    
IS there any processing is being done in Inventory constructor?

1 solution

The StackOverflowException is triggered by a recursive method that creates a deep call stack. The problem is linked to the concept of the stack memory region in general.

Basics : From Wikipedia[^]
Stack Overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.


How to Resolve: From MSDN[^]
Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.


I would suggest you to debug the program. Let it throw the exception. Once exception is thrown and then look at the stack trace. Your stack trace will have a pattern to it showing any number of repeating method calls. Figure out which one of the method calls should not be calling another one in the stack trace and your error will go away. If you are unable to find the pattern in your stack trace, just click on Improve question and provide the stack trace. We will help you on that.

[Edit 1]
Here I am adding a link which will help you to read stack trace.
Step by Step Guide to Trace the ASP.NET Application for Beginners[^]

--Amit
 
Share this answer
 
v2
Comments
Lancy.net 14-Jun-13 0:34am    
Thanks Amy I found the error
_Amy 14-Jun-13 0:37am    
Welcome. :) Will you accept the answer formerly(green button), if it worked for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900