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 ‘method’

BUG: Run-Time Error Message -2147417848 (80010108) When Passing Array of Dictionary Objects

Symptoms
You have a Visual Basic ActiveX DLL that has a method that takes an array of Scripting Dictionary Objects as an argument. It may work fine when your Visual Basic client is run in the Visual Basic integrated development environment (IDE) by using this method, but when you run it as a compiled application, you get the following run-time error message:

Run-time error ‘-2147417848 (80010108)’:
Method ‘~’ of object ‘~’ failedThis only occurs when you use late binding to call the method.
Resolution
Use early binding to work around the problem.

“Name ‘DTE’ is not declared” error message while running the “Item Method (General Extensibility)” MSDN sample code

Symptoms
This article describes problems that you may experience when you follow the documentation for the Item Method (General Extensibility) that appears in the following Microsoft Developer Network (MSDN) article:
http://msdn2.microsoft.com/en-us/library/aa301425(VS.71).aspx(http://msdn2.microsoft.com/en-us/library/aa301425(VS.71).aspx)When you paste the sample code from the Example section of the article to a Visual Basic .NET application, and then you try to compile the file, you receive the following error message:

Name ‘DTE’ is not declared.When you try to assign the return value of the EnvDTE.Documents.Item method to an AddIn type variable, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Specified cast is not valid.When you pass an Object parameter to the EnvDTE.Documents.Item method, you receive the following error message:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in ApplicationName.exe
Additional information: Type mismatch.
Resolution
You receive the “Name ‘DTE’ is not declared” error message because the variable named DTE is not declared in the sample code that is provided in the MSDN article. You receive theInvalidCastException error message because the MSDN article states that the return value for the EnvDTE.Documents.Item method is an AddIn type. However, the EnvDTE.Documents.Item method returns a Document type object. Therefore, when you try to assign a Document type object to an AddIn type variable, you receive the error message.You receive the Type mismatch error message when you try to pass a parameter that is not an Integer or a String to the EnvDTE.Documents.Item method. The EnvDTE.Documents.Item method is used to gain access to the elements of the EnvDTE.Documents collection. The EnvDTE.Documents.Item method can accept only two types of parameters:Integer – for the index value of the documents in the EnvDTE.Documents collectionString – for the key of the documents in the EnvDTE.Documents collection

PRB: Error When You Create SQL Server TEMP Tables Using Remote Data Objects (RDO)

Symptoms
When you create a SQL Server local temporary table using the rdoConnection object by calling its .Execute method with default parameters, and then attempt to access the table after the Remote Data Objects (RDO) method has run, you may receive one of the following error messages:

Run-time error ‘40002′: 37000:[Microsoft][ODBC SQL Server Driver][SQL Server] Statement(s)could not be prepared
-or-

Run-time error ‘40002′: S0002:[Microsoft][ODBC SQL Server Driver][SQL Server] Invalid Object Name ‘#<Name of the temporary table>’
Resolution
The creation and use of temporary database tables to facilitate the storage and manipulation of volatile intermediate data is a common programming practice. The default behavior of the SQL Server Open Database Connectivity (ODBC) driver is to create and use temporary stored procedures to run prepared statements. The .Execute method of the rdoConnection object uses the SQLPrepare() and SQLExecute() ODBC application programming interface (API) calls by default to run a SQL statement as a prepared statement. Temporary tables that are created by a stored procedure are automatically dropped when the procedure completes execution. As a result, when you attempt to access a SQL Server temporary table that was created by calling the .Execute method of an rdoConnection object with default parameters, in subsequent statements you receive one of the error messages specified in the “Symptoms” section.

PRB: Error “Syntax Error Near ‘Tablename’” on Recordset Update

Symptoms
With SQL Server’s quoted_identifier option set to Off, you may receive the following error:

Run-time error ‘-2147217900 (80040e14)’:
Line 1: Syntax error near ‘tablename’ This error occurs when you are using client-side cursors with the Microsoft OLE DB Provider for SQL Server (SQLOLEDB). The error occurs on an ActiveX Data Objects (ADO) recordset’s Update method and may occur on an AddNew method.
Resolution
With ADO client-side cursors, when you invoke an ADO recordset’s AddNew or Update method, the OLE DB Provider prepares a SQL statement to send to SQL Server.
The Microsoft OLE DB Provider for SQL Server automatically quotes identifiers on an ADO recordset’s Update method and may quote identifiers on an AddNew method. Identifiers include table names and field names.
For example, updating the Titles table in the Pubs database with the following code:

MyADORecordet.Update The preceding would be prepared similar to the following:

UPDATE “titles” SET “title”=’Hello World’ WHERE “title_id”=’3′ Note that the table name is in quotes, “titles”, and that each field name is in quotes, “title”, “title_id”, and so on.
If SQL Server’s Quoted_Identifier option is set to Off, SQL Server will not recognize table names and field names enclosed in quotes.
The error “Syntax error near ‘tablename’” occurs.

PRB: “System.Messaging.MessageQueueException” Error Message When You Run the MessageQueue.Send Method MSDN Sample Code or When You Run the MessageQueue.Receive Method MSDN Sample Code

Symptoms
When you run the sample code that appears on certain Microsoft Developer Network (MSDN) Web sites, you may receive the following error message:

An unhandled exception of type ‘System.Messaging.MessageQueueException’ occurred in system.messaging.dll
Additional information: External component has thrown an exception.The following MSDN Web sites are affected:
MessageQueue.Send Method
http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue.send(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue.send(vs.71).aspx)
MessageQueue.Send Method (Object)
http://msdn2.microsoft.com/en-us/library/aa329510(VS.71).aspx(http://msdn2.microsoft.com/en-us/library/aa329510(VS.71).aspx)
MessageQueue.Send Method (Object, MessageQueueTransaction)
http://msdn2.microsoft.com/en-us/library/aa329511(VS.71).aspx(http://msdn2.microsoft.com/en-us/library/aa329511(VS.71).aspx)
MessageQueue.Receive Method
http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue.receive(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.messaging.messagequeue.receive(vs.71).aspx)
MessageQueue.Receive Method (MessageQueueTransaction)
http://msdn2.microsoft.com/en-us/library/aa329505(VS.71).aspx(http://msdn2.microsoft.com/en-us/library/aa329505(VS.71).aspx)
MessageQueue.Receive Method (TimeSpan, MessageQueueTransaction)
http://msdn2.microsoft.com/en-us/library/aa329508(VS.71).aspx(http://msdn2.microsoft.com/en-us/library/aa329508(VS.71).aspx)
Resolution
The sample code that appears on these MSDN Web sites contains the following code that may cause you to receive the System.Messaging.MessageQueueException error message.
Microsoft Visual Basic .NET

myQueue.Send(“My Message Data.”, New _MessageQueueTransaction())Microsoft Visual C# .NET

myQueue.Send(“My Message Data.”, newMessageQueueTransaction());Microsoft Visual C++ .NET

myQueue->Send(S”My Message Data.”, new MessageQueueTransaction());You receive the error message because the call to the myQueue.Send method uses a MessageQueueTransaction object without starting a transaction. Additionally, even if you start a transaction before calling the myQueue.Send method, your application may wait indefinitely to receive the sent message. However, your application does not receive a sent message unless the corresponding transaction is committed.
Note The MessageQueue.Send(Object, MessageQueueTransaction) method applies only to transactional queues. Therefore, you receive the error message that is mentioned in the “Symptoms” sectiononly if you use transactional queues.

How to implement the Dispose method in a derived class in Visual Basic .NET or in Visual Basic 2005

Symptoms
When you author a class that extends a base class, you need to somehow handle the release of allocated resources. To do this, the Dispose method from the base class should be overridden in the derived classes.This article discusses common problems encountered in this scenario, how to properly override the Dispose method, and is meant to clarify some of the subtleties in the following Visual Basic .NET help article:
http://msdn2.microsoft.com/en-us/library/fs2xkftw(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/fs2xkftw(vs.71).aspx) Refer to this Help document for detailed information about error handling and for general examples of the Dispose method.
Resolution
A base class needs to contain an overloaded set of Dispose methods.The first instance of the sample code that follows is a version without parameters, and the second instance accepts a Boolean parameter:

‘Method that is called by Public to ensure TRUE is passed to DisposePublic Overloads Notoverridable Sub Dispose()Dispose( TRUE )’ Take yourself off of the finalization queue.GC.SuppressFinalize(Me)End Sub’Method that does the actual disposal of resourcesProtected Overloads Overridable Sub Dispose(ByVal disposing As Boolean)’Clean Up ResourcesEnd Sub
Dispose() is the method that is called when an object is disposed of in the code in which the object was created. This is a Public method, and therefore it can be used when an instance of the class exists.The Dispose() method then calls the Dispose(Boolean) method and passes a value of TRUE. The Dispose(Boolean) method is responsible for cleaning up the resources of the class.
When a class is derived from a base class, only the Dispose(Boolean) method needs to be overridden. All resource-cleanup for the derived class will be performed in this overridden method, and then the Dispose(Boolean) method for the base class is called. The following is a primitive example of the function overriding the base class:

Protected Overloads Overrides Sub Dispose(disposing As Boolean)’Clean Up ResourcesMyBase.Dispose( disposing )End Sub The derived class does not need a Dispose() method, because that method is inherited from the base class. When Dispose() is called on an instance of the derived class, Dispose() uses the Dispose(Boolean) of the derived class rather than the one in the base class. It is then important that the Dispose(Boolean) method of the derived class calls the Dispose(Boolean) method of the base class. This is done by means of the MyBase.Dispose(disposing) method. The Dispose(Boolean) method for the base class must be called to ensure that the resources of the base class are also disposed of.
Dispose() is meant as an entry point for public access to the disposal of an object and to ensure that TRUE is passed to the Dispose(Boolean) method. FALSE should be passed only when the Dispose(Boolean) method is called by the runtime or Finalize method. When FALSE is passed, only the unmanaged resources will be disposed. When TRUE is passed, both the managed and unmanaged resources are disposed.
The Visual Studio Development Environment inserts the code to override the Dispose() method into a class that inherits a system object (for example, Inherits System.Windows.Forms.TextBox). This is performed from the menus (at the top of the code window, by default) by selecting Overrides and then clicking Dispose(). The code that is inserted looks something like the following:

Public Overloads Overrides Sub Dispose()’Clean Up ResourcesEnd Sub If this is done, no compile errors are raised. However, when the derived class is loaded at runtime, you receive a runtime error message similar to the following:

An unhandled exception of type ‘System.TypeLoadException’ occurred in system.windows.forms.dll.
Additional information: Declaration referenced in a method implementation can not be a final method. Type: ClassLibrary1.UserControl1. Assembly: Dispose.NOTE: The Type value will be different from that in the preceding example. That is merely the name of the class that attempted to use an improperly overridden Dispose() method.
To correct this issue, just overload the Dispose(Boolean) method instead of Dispose(), and make sure that a call is made to the Dispose(Boolean) method of the base class and that TRUE is passed to it.
NOTE: In Visual Basic .NET or in Visual Basic 2005, the Overridable keyword is used like the Virtual keyword in C# and C++.Methods are, by default, NotOverridable.