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 for September, 2010

BUG: User Defined Type Not Defined Using Binary Compatibility

Symptoms
When compiling an ActiveX DLL or ActiveX EXE with Binary Compatibility, thefollowing error occurs:

Compile Error : User defined type not defined
Resolution
Turn off Binary Compatibility and compile the project. Visual Basic willhighlight the line of code that contains the User Defined Type that is notdefined. After resolving the problem, Binary Compatibility can be turnedback on.

BUG: T-SQL PRINT Statement May Not Show as Informational Error

Symptoms
You can use Microsoft SQL Server’s PRINT statement in stored procedures to return messages as informational errors in ADO, OLE DB, and ODBC applications. However, a Visual Basic client application may not capture such informational messages when it sets up a DataEnvironment command and uses the DataEnvironment.CommandName syntax to run the stored procedure.
Resolution
This problem has its roots in ADO. The InfoMessage event of an ADO Connection object does not fire when its CursorLocation property is set to adUseClient. By default, the CursorLocation property of Visual Basic 6.0 DataEnvironment Connection objects is set to adUseClient. As a result, the InfoMessage event procedure of DataEnvironment Connection objects does not fire when informational messages are returned to the client application.

BUG: The Visual Basic Upgrade Wizard stops responding when you try to upgrade a Visual Basic 6.0 project in Visual Basic 2005

Symptoms
Consider the following scenario. A Microsoft Visual Basic 6.0 project contains at least one code module thathas nine or more Case Is statements. You try to upgrade the Visual Basic 6.0 project by using the Visual Basic Upgrade Wizard in Microsoft Visual Basic 2005. In this scenario, the Visual Basic Upgrade Wizard stops responding (hangs).
Resolution
To work around this problem, follow these steps:Open the Visual Basic 6.0 project in Microsoft Visual Studio 6.0.Comment out anyCase Is statements.Save the code module.Start Visual Basic 2005.On the File menu, click Open Project.In the Open Project dialog box, locate the Visual Basic 6.0 project folder, click the project file name, and then click Open. The Visual Basic Upgrade Wizard starts.Click Next.Under Choose a Project Type, click EXE or DLL, and then click Next.Type a location for the project, and then click Next.If the folder does not exist, click Yes to create the folder.Click Next to start the upgrade.After the upgrade is complete, remove the comments from the Case Is statements.

BUG: The Visual Basic .NET or Visual Basic 2005 Upgrade Wizard reports an incorrect warning message for user-defined data types

Symptoms
If you use Visual Basic Upgrade Wizard on your Microsoft Visual Basic 6.0 project with a user-defined data type, the wizard generates a warning message for the code with user-defined data type assignments. For example, you may receive the following warning message for a direct user-defined data type assignment.

UPGRADE_WARNING: Could not resolve default property of object <<UDT>>. Click for more information: ‘ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword=”vbup1037′
Resolution
The wizard incorrectly treats the user-defined data type variables as a class object, and then tries to locate the default property for the object. The user-defined data type does not have default properties. Therefore, the wizard cannot locate a default property, and it reports an incorrect warning.

BUG: The Text property of a data-bound ComboBox returns an incorrect value when you set the value in code

Symptoms
If you programmatically set the Text property for a data-bound member of the ComboBox control, the value typed in the text box portion of the ComboBox is displayed as expected. However, the Text property and the SelectedIndex property incorrectly return the value of the last item that was selected in the list box instead of returning the value typed in the text box.
When you type in the ComboBox at runtime, Text property returns the typed value, and SelectedIndex returns a value of -1.
Resolution
To resolve this problem, set the SelectedIndex property to -1 before you set the Text property for a data-bound member of the ComboBox, as in the following examples. Visual Basic .NET

ComboBox1.SelectedIndex = -1ComboBox1.Text = “My Text” Visual C# .NET

ComboBox1.SelectedIndex = -1;ComboBox1.Text = “My Text”; NOTE: Do not use the Text property to select a data-bound member of the ComboBox. You must locate the item in the list that you want to show, and then set the SelectedIndex to the index of the item. You do not have to set the Text property.
For example, if you bind the DisplayMember property and the ValueMember property of the ComboBox to a list that contains the numbers 1 through 10, and you want the ComboBox to display the number 5 in the text box and show 5 as selected in the list, you must set the SelectedIndex property to 5.

BUG: The original data appears in the DataGrid control although you have removed the data from the DataSet object

Symptoms
To make your data appear on a form, you bind a Microsoft Windows Forms DataGrid control to a DataTable object that is included in a DataSet object.
After your data has appeared in the DataGrid control, you use the Tables.Clear method to remove the DataTable object from the Tables collection of the bound DataSet object. However, although the bound DataSet object no longer contains any data, your original data continues to appear in the DataGrid control.
Resolution
In a Microsoft Windows-based application, the CurrencyManager object manages a list of Binding objects. When you use a DataSet object together with a DataTable object for your data source, and you bind a DataGrid control to the DataSet object and the DataTable object, the CurrencyManager object manages this data source.
However, if you remove the DataTable object from the DataSet object, the related the CurrencyManager object that corresponds to the DataSet object is not updated. Therefore, the DataGrid control does not have this new information and the original data continues to appear in the DataGrid control.