Microsoft KB Archive/316319

= FIX: System.StackOverflowException Error When You Access the UnderlyingValue Property =

Article ID: 316319

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q316319



Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:

840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



SYMPTOMS
When you try to access the Underlying Value property of a field by using ActiveX Data Objects (ADO) with the Component Object Model (COM) interoperability layer, you may encounter the following exception:

An unhandled exception of type 'System.StackOverflowException' occurred in adodb.dll



RESOLUTION
To work around this problem, declare the variable as type Object instead of Recordset to late bind the Recordset object, as in the following example: Dim rs As Object = New Recordset



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Windows .NET Framework 1.1.



Steps to Reproduce the Problem
 Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. Place a Button control on Form1, and change its Name property to btnTest . In Solution Explorer, right-click References, and then click Add Reference. On the .NET tab in the Add Reference dialog box, click ADODB.dll, click Select to add this reference to the Selected Components section, and then click OK.</li>  Use the Imports statement on the ADODB namespace so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the &quot;General Declarations&quot; section of Form1: Imports ADODB </li>  Copy and paste the following code in the code window after the &quot;Windows Form Designer generated code&quot; region: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cn As New Connection

'Declare the variable as RecordSet Dim rs As New Recordset 'Comment the previous line and uncomment the next line of Code to declare the variable as Object 'Dim rs As Object = New Recordset

Dim fld As Field Dim Conn As String Dim DelQuery As String Dim CreateQuery As String Dim InsQuery As String Dim SelQuery As String

Conn = &quot;Provider=SQLOLEDB.1;User ID=username;Password=password;&quot; & _ &quot;Initial Catalog=pubs;Data Source=ServerName&quot;

DelQuery = &quot;If exists (Select * from SysObjects where id = object_id(N'[dbo].[TestTable]')&quot; & _       &quot;and OBJECTPROPERTY(id, N'IsUserTable') = 1)&quot; & _ &quot;Drop table [dbo].[TestTable]&quot; CreateQuery = &quot;Create table TestTable (fld1 integer primary key, fld2 varchar(20))&quot; InsQuery = &quot;Insert into TestTable values (1, '1')&quot; SelQuery = &quot;Select * from TestTable&quot;

cn.CursorLocation = CursorLocationEnum.adUseClient cn.Open(Conn) 'Open Connection cn.Execute(DelQuery) 'Delete table if it already exists cn.Execute(CreateQuery) 'Create a new table cn.Execute(InsQuery) 'Insert data

rs.Open(SelQuery, cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockOptimistic) rs.Fields(1).Value = &quot;Test&quot; Debug.WriteLine(rs.Fields(1).Value) Debug.WriteLine(rs.Fields(1).UnderlyingValue) 'This line will cause the above mentioned exception End Sub </li> Modify the connection string (Conn) as appropriate for your environment.</li> Save your project.</li> On the Debug menu, click Start to run your project.

Note that the code fails with the above-mentioned exception when you try to access the UnderlyingValue property of the field.</li> Create the Recordset variable as an Object.</li> On the Debug menu, click Start to run your project</li></ol>

<div class="references_section">