Microsoft KB Archive/832947

= BUG: An exception occurs in the ActiveX Data Objects library when you execute a query a second time in a Visual Basic .NET application =

Article ID: 832947

Article Last Modified on 6/15/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-





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 execute a query from a Microsoft Visual Basic .NET application a second time, the Microsoft ActiveX Data Objects Library (ADODB) throws an exception, and you receive the following error message:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll

Additional information: Syntax error or access violation



WORKAROUND
To work around this problem, use an early bound expression. To do this, locate the following code in step 6 of the &quot;More Information&quot; section of this article. cn.QueryCustomers(intDisc, rs)   '==>> exception here rs.ActiveConnection = Nothing Replace this code with the following code. rs = cmd.Execute(, New Object {intDisc})



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates. In the Name box, type SampleWinApp, and then click OK. Add a Button control to the Form1.vb form.</li>  Add the following code to the OnClick event procedure of the button. Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim cmd As ADODB.Command Dim intDisc As Short Dim ts As Integer

cn = New ADODB.Connection cn.Open(&quot;provider=sqloledb;data source=legend;initial catalog=pubs;Integrated Security=SSPI&quot;) intDisc = 0 cmd = New ADODB.Command With cmd .Name = &quot;QueryCustomers&quot; .CommandText = &quot;Select top 5 stor_id as CustName, Discount from discounts where discount > ?&quot; .CommandType = ADODB.CommandTypeEnum.adCmdText .Parameters.Append(.CreateParameter(&quot;DiscountIn&quot;, ADODB.DataTypeEnum.adDouble, ADODB.ParameterDirectionEnum.adParamInput)) .ActiveConnection = cn   rs = New ADODB.Recordset rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic cn.QueryCustomers(intDisc, rs)   '==>> exception here rs.ActiveConnection = Nothing cmd.ActiveConnection = Nothing End With MsgBox(&quot;Successful&quot;) </li> On the Project menu, click Add Reference.</li> In the Add Reference dialog box, click the adodb component on the .NET tab, click Select, and then click OK.</li> On the Debug menu, click Start to run the application.</li> Click the button on the Form1 form. You receive a message that indicates that the code was executed successfully.</li> Click the button a second time. The exception error message that is described in the &quot;Symptoms&quot; section appears.</li></ol>

<div class="references_section">