Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘microsoft visualbasic’

FIX: Error in COleDispatchException Constructor

Symptoms
In an MFC application, AfxThrowOleDispatchException is used to throw anexception from within an OLE Automation function. When Microsoft VisualBasic is used as the automation client application, it will not be able tocorrectly interpret the error code passed to AfxThrowOleDispatchException.
Resolution
AfxThrowOleDispatchException constructs and throws a COleDispatchExceptionobject, which is used to handle exceptions specific to the OLE IDispatchinterface. The constructor of COleDispatchException incorrectly initializesthe COleDispatchException object, resulting in the Visual Basic clientbeing unable to interpret the error code of the exception.

How to enable the “Option Strict” and “Option Explicit” statements in Visual Basic applications

Symptoms
This article describes how to enable the Option Strict and Option Explicit statements in Microsoft Visual Basic applications for code-behind files and files that are contained in the App_Code folder.
Resolution
To enable the Option Strict and Option Explicit statements in the code-behind files and files that are contained in the App_Code folder of a Visual Basic application, override the compiler configuration in the system.codedom section of the Web.config file for the application. To do this, add the following code to the Web.config file:

<system.codedom> <compilers> <compiler language=”c#;cs;csharp” extension=”.cs” type=”Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ warningLevel=”0″/> <compiler language=”vb;vbs;visualbasic;vbscript” extension=”.vb” type=”Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ warningLevel=”0″ /> </compilers> </system.codedom>

BUG: You receive a “Syntax error or access violation” error message in ADO.NET when you run a query two times

Symptoms
When you use the ActiveX Data Objects (ADO) Connection object in Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to run a SQL query that is associated with the ADODB.Command object two times consecutively, you may receive the following error message similar to:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in microsoft.visualbasic.dll
Additional information: Syntax error or access violation
Resolution
When you use the ADODB.Connection object to run a SQL query that is associated with the ADODB.Command object, late binding is used. The error that is mentioned in the “Symptoms” section occurs because late binding the COM interop caches the results of the GetIDsOfNames method of the IDispatch Interface.

BUG: You receive a “Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall” error message when you make a late-bound call in Visual Basic .NET

Symptoms
When you make a late-bound call in Microsoft Visual Basic .NET, the late-binding support fails when the ByRef decimal is converted to ByRef currency. You receive the following error message:

Unhandled Exception: System.Runtime.InteropServices.COMException (0×80020005): Type mismatch. at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn) at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack) at ConsoleApplication2.Module1.main() in C:\ConsoleApplication\Module.vb:line number
Press any key to continueNote In this error message, C:\ConsoleApplication\Module.vb:line number is a placeholder for the actual path where the application is saved. In the application module, line number represents the line where the error occurs.
Resolution
To work around this bug, use either of the following options: Use the CurrencyWrapper type.Force the decimal value to be passed by using the ByVal keyword.
To do this, put parentheses around the variable that contains the decimal value.To implement either of these workaround options, create a DLL by using Visual Basic 6.0, create a console application, and reference the DLL in the console application, as follows:Create a DLL by using Visual Basic 6.0Start Visual Basic 6.0.On the File menu, click New Project.
The New Project dialog box appears.Click ActiveX DLL, and then click OK.
By default, the Class1 class is created.Add the following code to the Class1 class:

Public Function cedrbank(name As String, ACCNUM As String, ACCBAL As Currency) As LongMsgBox ACCBALACCBAL = 4.321End FunctionOn the File menu, click Save Class1.cls, and then click Save Project As.In the File name box, type the name of the project.On the File menu, click Make Project.dll.
The Make Project dialog box appears.
Note Project is a placeholder for the actual name of the project.Click OK.Create a console applicationStart Visual Studio .NET.On the File menu, point to New, and then click Project.
The New Project dialog box appears.Under Project Types, click Visual Basic Projects.Under Templates, click Console Application.Click OK.
By default, the Module1.vb module is created.Replace the existing code in the Module1.vb module with the following code:

Imports SystemImports System.Runtime.InteropServicesImports System.ReflectionImports Project1Public Module Module1Sub Main()Dim x As Objectx = New Class1Dim dc As Object = New CurrencyWrapper(5)Dim d As Decimal = 1.987Dim args(2) As Objectargs(0) = “name”args(1) = “num”args(2) = dcDim c(0) As Reflection.ParameterModifierc(0) = New Reflection.ParameterModifier(3)c(0).Item(0) = Truec(0).Item(1) = Truec(0).Item(2) = TrueCObj(x).GetType().InvokeMember(“cedrbank”, BindingFlags.InvokeMethod, Nothing, x, args, c, Nothing, Nothing)Console.WriteLine(args(2))dc = New CurrencyWrapper(6)’Use the CurrencyWrapper type.x.cedrbank(“name”, “num”, dc)Console.WriteLine(dc)’Force ‘d’ to be passed ByVal by using parentheses around ‘d.’x.cedrbank(“name”, “num”, (d))Console.WriteLine(d)End SubEnd ModuleAdd a reference to the DLL that you created in the “Create a DLL by using Visual Basic 6.0″ section of this article.On the Build menu, click Build Solution.On the Debug menu, click Start.
You do not receive the error message that is mentioned in the “Symptoms” section of this article.

BUG: “Type System.IO.IOException” error message after you upgrade to Visual Basic .NET or Visual Basic 2005

Symptoms
After you upgrade your Microsoft Visual Basic 6 application to Visual Basic .NET or Visual Basic 2005, you may receive the following “exception” error message:

An unhandled exception of the type ‘System.IO.IOException’ occurred in microsoft.visualbasic.dll.
Resolution
This behavior occurs when all of the following conditions are true: The Visual Basic Upgrade Wizard upgrades Visual Basic 6 code that uses the Open statement to open a given file.
-and-You do not explicitly specify shared access to open this file.
-and-You attempt to open this file a second time without first closing it.NOTE: You may also receive this error message if you try to open a given file a second time (without closing the first file handle) by using the FileOpen function in Visual Basic .NET or in Visual Basic 2005 without specifying the Share argument.

BUG: “InteropServices.COMException” error message occurs when you pass ByRef Parameters by using late binding

Symptoms
When you call a COM component in Visual Basic .NET, and you use late binding, you may receive the following error message:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in microsoft.visualbasic.dll. Additional information: Type mismatch.This behavior occurs when all the following conditions are true: The method that is called is a Public method in the COM component. The method takes a ByRef parameter of type Object.You use late binding to call the COM component.
Resolution
When you run an application, the .NET Framework late binding mechanism tries to pass the ByRef parameters to the COM component as objects. The COM component expects the parameters to be of the type IDispatch. The .NET Framework raises a type mismatch exception. This occurs because the .NET Framework cannot convert the uninitialized objects to the IDispatch type. The early binding signature of the method in the COM component follows:

[id(0x60030000)]HRESULT MyFunc([in, out] IDispatch** var1);