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 visual studio’

How To Associate a Custom Icon with a Formless Visual Basic Application

Symptoms
For Visual Basic applications that do not contain forms, you can still provide a custom icon for the executable. This article explains how you can add an icon resource to the project.
Resolution
When you create a Visual Basic executable application, you can select an icon from one of the project’s forms in the Icon drop-down list box on the Make tab of the Project Properties dialog box to use as the icon for the executable file. However, if there are no forms in the project, no icons are available in the drop-down list box of the Make tab. In this case, you can create a custom icon resource in your project. The compiler uses this resource as the icon for your executable file. If you include more than one icon, the compiler uses the icon whose letter appears first in the alphabet because it prioritizes alphabetically.
The following sample demonstrates how to provide a custom icon for a simple, formless Visual Basic EXE project. For this sample, you may select an icon from your system, or you can create your own. By default, Visual Basic 6.0 installs icons in the following folder:
C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\ To create your own icon, you can use a tool such as Image Editor, which is available from the Visual Basic 6.0 CD (Disk 1) or the Visual Studio 6.0 CD (Disk 3) at the following location:
\Common\Tools\VB\Imagedit\Imagedit.exe
Step-by-Step ExampleCreate a new Visual Basic standard EXE project. Form1 is created by default.Right-click Form1 in the Project window, and then click Remove Form1 to remove the form from the project.Add a new standard module (Module1) to the project.Paste the following code into the code window of Module1:

Public Sub Main()MsgBox “Hello World”End Sub Select an icon from the list, or use Image Editor (or another appropriate tool) to create an icon file.To load the Visual Basic Resource Editor, follow these steps:From the Add-Ins menu, click Add-In Manager.Locate VB 6 Resource Editor in the list of available add-ins.Double-click VB 6 Resource Editor to load the editor add-in.From the Tools menu, click Resource Editor.In the Resource Editor window, click Add Icon on the toolbar. (This button appears as a small gray square that is outlined in blue.)Open the icon file that you created earlier.By default, the icon is added with the name “101″. Right-click the icon resource that was just added, and then click Properties.In the Id box, type APPICON to rename the icon resource, and then click OK.
NOTE: “APPICON” is just a suggested name for your resource. If you already have other, named icon resources, make sure that your executable icon begins with a letter that occurs later in the alphabet than the other icons. For example, if you have an icon that is named “AAA” and another that is named “BBB”, the compiler uses the one that is named “AAA” as your application icon.From the File menu, click Make to compile the Visual Basic project.In Windows Explorer, browse to the location where you compiled your executable file. Notice that the icon for the executable file is the icon that you selected in the Resource Editor.

Error message occurs when you run commands on a command object: “Unhandled exception of type ‘System.InvalidOperationException’”

Symptoms
If you run commands or call methods of the SqlCommand or OleDbCommand object, you receive the following error message if a connection is not open:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in system.data.dll
Additional information: ExecuteReader requires an open and available Connection (state=Closed).
Resolution
The DataAdapter object does not require that you explicitly open a connection to run some of its methods. Therefore, you can call the Update or Fill method of the DataAdapter object when the connection is closed. The Connection object that is associated with the SELECT statement must be valid, but it does not need to be open. If you close the connection before you call Fill, the connection is opened to retrieve the data and then closed. If the connection is open before you call Fill, it remains open.
Steps to Reproduce the BehaviorStart Microsoft Visual Studio .NET.Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.Place two Button controls and one DataGrid control on Form1. Button1, Button2, and DataGrid1 are created by default.Change the Name property of Button1 to btnDataAdapter and the Text property to DataAdapter.
Change the Name property of Button2 to btnCommand and the Text property to Command.Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the “General Declarations” section of Form1:

Imports SystemImports System.Data.OleDbImports System.Data.SqlClient In the Code window, copy and paste the following code after the “Windows Form Designer generated code” region:

Private Sub btnDataAdapter(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnDataAdapter.ClickDim myConnString As String = _”User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=myServer”Dim mySelectQuery As String = _”Select * From Customers Where CustomerID Like ‘A%’”Dim con As New SqlConnection(myConnString)’The code works fine even if you comment out the next line (to open the connection).con.Open()Dim daCust As New SqlDataAdapter(mySelectQuery, con)Dim ds As New DataSet()daCust.Fill(ds, “Cust”)DataGrid1.DataSource = dsDataGrid1.DataMember = “Cust”End SubPrivate Sub btnCommand(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnCommand.ClickDim myConnString As String = _”User ID=sa;password=sa;Initial Catalog=Northwind;Data Source=myServer”Dim mySelectQuery As String = _”Select * From Customers Where CustomerID Like ‘A%’”Dim con As New SqlConnection(myConnString)Dim myCommand As New SqlCommand(mySelectQuery, con)’An exception is thrown if you comment out the next line (to open the connection).con.Open()Dim myReader As SqlDataReader = myCommand.ExecuteReader()While myReader.Read()’Process data.End WhilemyReader.Close()con.Close()End Sub Modify the connection string (myConnString) as appropriate for your environment.Save your project. On the Debug menu, click Start to run your project.Comment out the line of code that opens the connection. Notice that DataAdapter.Fill works as expected, but Command.ExecuteReader fails with the above-mentioned exception.

PRB: Unexpected Error 40042119 with PDW: There Is No PostInfo File on the Server

Symptoms
When deploying to a Web server using the Package and Deployment Wizard (PDW), you see the following error:

Unexpected error number 40042119 has occurred: There is no PostInfo file on the server.
This error may occur if you have typed an incorrect URL or if the URL does not refer to a correctly configured Web server. Ensure that the URL is correct and the Web server is properly configured. For proper configuration, you may need to install Microsoft Visual Studio’s server-side setup on the Web server.
Resolution
This error occurs because of the following reasons: The Web service is not running.The PostInfo.asp file is missing or is not located in the InetPub\scripts physical directory.You do not have NTFS Read and Execute (RX) permissions to the InetPub\scripts directory.The <META> tag in default document (typically named Default.htm) in the root directory of the Web is pointing to the incorrect location for PostInfo.asp.

BUG: You may receive a “Visual Basic internal compiler error” error message when you use the ADDHANDLER statement or the REMOVEHANDLER statement in Visual Studio .NET

Symptoms
You can pass an array as a base reference to the event in the ADDHANDLER statement or in the REMOVEHANDLER statement in your application. However, when you try to compile your application, the compiler stops responding instead of displaying compilation errors. When you close the Microsoft Visual Studio .NET IDE, you may receive the following error message:

Visual Basic .NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler error)
Save your work and restart Visual Studio .NET.
Resolution
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

BUG: When You Remove Visual C# .NET an Error May Occur If You Try to Install Visual Studio Tools for Office

Symptoms
When you try to install Microsoft Visual Studio Tools for the Microsoft Office System on a computer with Microsoft Visual Basic .NET Standard 2003 installed, you may receive the following error message:

Setup cannot detect the required version of Visual Studio .NET on this computer.Before you install this product, you must install a matching regional version of Visual Studio .NET 2003 or Visual Basic .NET 2003 Standard.
Resolution
This error may occur if all the following conditions are sequentially true:Visual Basic .NET Standard 2003 is installed on the computer.Microsoft Visual C# .NET Standard 2003 is installed onthe computer.Visual C# .NET Standard 2003 isremoved.You try to installVisual Studio Tools for Office.

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.