StackOverflowexception

G

Guest

Hi all,
I don't know what the exact meaning in my case for this exception to be
thrown.
When my program execute the following statement:

item.NumPersonsOnTrxn = GetNumofPeoplePerTrxn(orderID, sProduct)

The system.stackoverflowexception was thrown.
The getNumberofPeoperPerTrxn is just a function defined as follow
Public Shared Function GetNumofPeoplePerTrxn(ByVal OrderID As String, ByVal
Product As String) As Integer

and the item.NumPersonsOnTrxn is an Integer property define in a class
ProducerTabularReport(Item).
Can you give me a hint? Before the exception, the function
GetNumofPeoplePerTrxn(orderID, sProduct) return 2 in watch window,
item.NumPersonsOnTrxn is 0(byDefault I guess)
 
G

Guest

..When this exception is thrown, I only have one pending method call on call
stack window which is the calling function for GetNumofPeoplePerTrxn.
 
K

Kevin Spencer

Well, the first thing I can tell you is that the problem with the function
is not in the signature of the function, but in the code of the function.
That's what you need to post.

A stack overflow is usually caused by an infinite loop or infinite
recursion.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
 
G

Guest

Hi Kevin,
Here is the funciton. I just cannot see there will be an infinite loop here.
Thanks for your help.

Public Shared Function GetNumofPeoplePerTrxn(ByVal OrderID As String, ByVal
Product As String) As Integer
Dim nameArr(3) As String
Dim i, j As Integer
i = 1
Dim sqlConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings(Global.CfgKeyConnString))
Dim myCommand As SqlCommand = New
SqlCommand("getNumPersonPerTrxn", sqlConnection)

myCommand.CommandType = CommandType.StoredProcedure
Dim pOrderID As SqlParameter = New SqlParameter("@OrderID",
SqlDbType.VarChar, 18)
pOrderID.Value = OrderID
myCommand.Parameters.Add(pOrderID)

Dim pProduct As SqlParameter = New SqlParameter("@Product",
SqlDbType.VarChar, 30)
pProduct.Value = Product
myCommand.Parameters.Add(pProduct)

sqlConnection.Open()
Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
'expect only one record or none
If result.Read() Then
Select Case Product
Case "Travel Insurance Select"
If
CInt(result.GetValue(result.GetOrdinal("SelectPlus"))) = 1 Then
'code will be add here

End If
Case "InterMedical Insurance"
If
Trim(CStr(result.GetValue(result.GetOrdinal("SpouseSurname")))) <> "" Then
i = i + 1
End If
For j = 1 To 3
If
Trim(CStr(result.GetValue(result.GetOrdinal("Child" & j & "Surname")))) <> ""
Then
i = i + 1
End If
Next
End Select
End If
Return i
End Function
 
G

Guest

Hi all,
Maybe the problem came from calling function? Here is the code for my
calling function since the function call GetNumofPeoplePerTrxn(orderID,
sProduct)
is inside the for loop.

Because I after put try...end try block, I got :
Timeout expired. The timeout period elapsed prior to obtaining a connection
from the pool. This may have occurred because all pooled connections were in
use and max pool size was reached.

But I close the connection in GetNumofPeoplePerTrxn(orderID, sProduct) call.
SqlHelper is download from report start kits(asp 1.1), I didn't change
anything.
It has cn.dispose for each cn.open() I don't know the difference between
close and dispose.
/////' the following is my calling function
Public Shared Function GetProductDetailedReport(ByVal AgentID As String,
ByVal Product As String, ByVal StartDate As DateTime, ByVal EndDate As
DateTime) As ProducerTabularReportCollection
Dim dsData As DataSet =
SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings(Global.CfgKeyConnString), "GetProductDetailedReport", AgentID, Product, StartDate, EndDate)
Dim items As New ProducerTabularReportCollection
Dim orderID As String
Dim sProduct As String


Dim row As DataRow
For Each row In dsData.Tables(0).Rows
Dim item As New ProducerTabularReport
item.AgentID = row("AgentID").ToString()
item.OrgName = row("Orgname").ToString()
item.Product = row("Product").ToString()
item.txnNumber = row("txnNumber").ToString()
orderID = Trim(row("Order_ID").ToString)
sProduct = Trim(row("Product").ToString())
Try
item.NumPersonsOnTrxn = GetNumofPeoplePerTrxn(orderID,
sProduct)
'End Select
Catch ex As Exception

Console.WriteLine(ex.Message)


End Try




item.TxnDate = Convert.ToDateTime(row("txnDate"))
item.txnSale = Convert.ToDecimal(row("txnSale"))
items.Add(item)
Next
Return items
End Function
 
G

Guest

Hi all,
Now I caught something:

When I step through I got infinite loop of execution of these two lines

Set(ByVal Value As Integer)
NumPersonsOnTrxn = Value
End Set

No wonder it throw stackoverflow when it execute: item.NumPersonsOnTrxn =
GetNumofPeoplePerTrxn(orderID, sProduct).
But I still don't know how it happens.

( I also got infinite lines of the following in call stack window)

CSRreport.CSRreports.Components.ProducerTabularReport.set_NumPersonsOnTrxn(Integer Value = 1) Line 140 + 0xc bytes Basic
 
G

Guest

Ok I caught it. That's because my silly typo cause this nasty problem. I
guess I shouldn't work on Fri.

I have a property in the class which looks like this:
Private _NumPersonsOnTrxn As Integer

Public Property NumPersonsOnTrxn() As Integer
Get
Return _NumPersonsOnTrxn
End Get
Set(ByVal Value As Integer)
NumPersonsOnTrxn = Value 'look at here, I typed
NumPersonsOnTrxn instead of _NumPersonsOnTrxn and then it cause recursive
call the Property NumPersonsOnTrxn.
'what a nasty mistake!

End Set
End Property
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top