I have an StackOverflowException in one of my DB functions that I don't know how to deal with. I have a SQLite database with one table "tblEmployees" that holds records for each employees (several thousand posts) and usually this function runs without any problem.
But sometimes after the the function is called a thousand times it breaks with an StackOverflowException at the line "ReturnTable.Load(reader)" with the message: An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.SQLite.dll
If I restart the application it has no problem to continue with the exact same post it last crashed on. I can also make the exactly same DB-call from SQLite Admin at the crash time without no problems.
Here is the code:
Public Function GetNextEmployeeInQueue() As String
Dim NextEmployeeInQueue As String = Nothing
Dim query As [String] = "SELECT FirstName FROM tblEmployees WHERE Checked=0 LIMIT 1;"
Try
Dim ReturnTable As New DataTable()
Dim mycommand As New SQLiteCommand(cnn)
mycommand.CommandText = query
Dim reader As SQLiteDataReader = mycommand.ExecuteReader()
ReturnTable.Load(reader)
reader.Close()
If ReturnTable.Rows.Count > 0 Then
NextEmployeeInQueue = ReturnTable.Rows(0)("FirstName").ToString()
Else
MsgBox("No more employees found in queue")
End If
Catch fail As Exception
MessageBox.Show("Error: " & fail.Message.ToString())
End Try
If NextEmployeeInQueue IsNot Nothing Then
Return NextEmployeeInQueue
Else
Return "No more records in queue"
End If
End Function
When crashes, the reader has "Property evaluation failed." in all values.
I assume there is some problem with allocated memory that isn't released correctly, but can't figure out what object it's all about. The DB-connection opens when the DB-class object is created and closes on main form Dispose.
Should I maybe dispose the mycommand object every time in a finally block? But wouldn't that result in a closed DB-connection?