Search code examples
c#sql-serverado.netsql-server-2012stack-overflow

SQL Server 2012 upgrade: StackOverflowException with ExecuteReader()


SOLVED: This was not an issue with SQL Server but a recursive function that kept calling itself. /FACEPALM

I am currently running SQL Server 2005 and using this code below. All it does is get the date and time from SQL Server and it works PERFECTLY.

protected internal static string GetDateAndTime()
{
string strDate = null;
string strSQL = null;
// connect to the database and execute our SQL command
strSQL = "SELECT GETDATE() AS THEDATE";
using (SqlConnection sqlConn = new SqlConnection(Constants.CONNECTION_STRING))
{
       using (SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn))
       {
              sqlConn.Open();
              using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
              {
                     if (sqlReader.HasRows)
                         {
                         while (sqlReader.Read())
                         {
                                // assign query returned row values
                                strDate = sqlReader["THEDATE"].ToString();
                         }
                     }
              }
        }
}
}

Upgraded to SQL Server 2012 and now it errors. (Surprise!) The code stops running at this line.

using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())

The errors are:

-StackOverflowException was unhandled
-An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll
-Make sure you do not have an infinite loop or infinite recursion.

Please note that I did not change ANY code except for pointing the connection string to the new SQL Server 2012 database.

REQUESTED INFO, Stack Trace:

[External Code] 
     MyProject.exe!MyProject.Database.GetDateAndTime() Line 173 C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.Database.GetDateAndTime() Line 189 + 0x8a bytes C#
MyProject.exe!MyProject.frmSwitchboard.VerifyUserExists() Line 230 + 0x9 bytes  C#
MyProject.exe!MyProject.frmSwitchboard.MyProjectPreLoad() Line 136 + 0xd bytes  C#
MyProject.exe!MyProject.frmSwitchboard.frmSwitchboard_Load(object sender = {MyProject.frmSwitchboard}, System.EventArgs e = {System.EventArgs}) Line 68 C#
[External Code] 
MyProject.exe!MyProject.Program.Main() Line 18  C#
[External Code] 

REQUESTED INFO, Constants.CONNECTION_STRING: *Tried using both...

1. protected internal static string DATABASE_CONNECTION_STRING = @"Data Source=DBSTEST;Initial Catalog=restored 02-04-2013;User ID=sa; UID=sa;PWD=blah;Persist Security Info=True";
2. protected internal static string DATABASE_CONNECTION_STRING = @"Data Source=DBSTEST;Initial Catalog=restored 02-04-2013;User ID=sa;UID=sa;PWD=blah;Connect Timeout=300";

REQUESTED INFO, function VerifyUserExists():

private DataTable VerifyUserExists()
{
        string strSQL = null;
        DataTable dt = null;
            strSQL = "SELECT DISTINCT [Employees].EmployeeID FROM [Employees] WHERE [Employees].Logon = 'User1'";
            dt = new DataTable();
            dt = Database.RunQueryAndReturnAsDataTable(strSQL);
}

REQUESTED INFO, RunQueryAndReturnAsDataTable():

protected internal static DataTable RunQueryAndReturnAsDataTable(string strSQL)
    {
        DataTable dt = null;
            // put our query into a datatable and return it
            using (SqlConnection sqlConn = new SqlConnection(Constants.DATABASE_CONNECTION_STRING))
            {
                using (SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn))
                {
                    sqlConn.Open();
                    using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
                    {
                        if (sqlReader.HasRows)
                        {
                            dt = new DataTable();
                            dt.Load(sqlReader, LoadOption.OverwriteChanges);
                        }
                    }
                }
            }
    }

The code stops running at the same line here too when I play around with the code, generating the same error.

using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())

Has anyone run into this issue? How can I fix this? Please advise, thank you!


Solution

  • The line you got there is wrong somehow. The stacktrace shows that the following function is performing recursion:

    MyProject.Database.GetDateAndTime
    

    It is calling itself without intermediaries (assuming you are running in Debug mode no function can disappear due to inlining).

    If you post its code I'll take a look. Probably, you can figure this out now.