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

INFO: New Tracing Feature in ASP.NET

Symptoms
This article provides information about the new tracing feature in ASP.NET. This article also describes how to configure this tracing feature.
Resolution
The tracing feature allows you to track the execution of an application and view the results. You can enable tracing at the page level or the application level. Enable Tracing at the Page Level When you enable tracing at the page level, the trace output is added to the bottom of the page. The following code sample demonstrates how to enable tracing at the page level:
Visual Basic .NET

<%@Page Language=”vb” Trace=”true”%><script runat=”server”>Public Function TracingTest(strNow as String) as StringTrace.Write(“Now() Value in the function”, now())return “The time is: ” & strNowend Function</script>TracingTest result: <%=TracingTest(Now())%> Visual C# .NET

<%@ Page Language=”c#” Trace=”true”%><script runat=”server”>public string TracingTest(string strNow){Trace.Write(“Now() Value in the function”,DateTime.Now.ToString());return “The Time is: ” + strNow;}</script>Tracing Test Results:<%=TracingTest(DateTime.Now.ToString())%> Visual J# .NET

<%@ Page Language=”VJ#” Trace=”true”%><%@ import namespace = “System.Diagnostics” %><%@ import namespace = “System” %><script runat=”server”>public System.String TracingTest(System.String strNow){Trace.Write(“Now() Value in the function”,DateTime.get_Now().ToString());return “The Time is: ” + strNow;}</script>Tracing Test Results:<%=TracingTest(DateTime.get_Now().ToString())%> When you run this page, both of the results from the function are written to the browser. You also see the information that tracing returns.
To remove the trace information, set the Trace attribute of the @ Page directive to false. You do not need to remove the Trace.Write statement. Notice that this is the TraceContext class, not the Trace class.
To enable tracing at the page level in Microsoft Visual Studio .NET, you can also set the trace property of the document to true in the Properties window. Enable Tracing at the Application Level To enable tracing at the application level, use the Web.config file. The following code sample demonstrates how to configure tracing at the application level in the Web.config file:

<configuration><system.web><trace enabled=”true” requestLimit=”10″ pageOutput=”true”traceMode=”SortByTime” localOnly=”true”/></system.web></configuration> When you enable tracing at the application level, you can use the pageOutput attribute to specify whether the trace output is displayed at the page level. If you set pageOutput to true, the output displays the same results as if you set the Trace attribute of the @ Page directive at the top of the page to true.
NOTE: The trace setting at the page level overrides the trace setting at the application level.

How to consume assemblies that are located in a folder that is different from the application base folder in Visual Basic .NET or in Visual Basic 2005

Symptoms
This article discusses how to consume assemblies that are located in a folder than is different from the application base folder in a Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 solution.
Resolution
When you run a Visual Basic .NET or Visual Basic 2005 application, the common language runtime (CLR) expects to locate any assemblies that the application uses in either the global assembly cache or the application folder. When the CLR does not successfully bind to an assembly, you receive an error message that is similar to the following:

System.IO.FileNotFoundException. File or assembly name AssemblyName, or one of its dependencies, was not found. You can use many methods to locate an assembly that is located in a different folder. This article describes the following three methods.
Method 1: Install the assemblies in the global assembly cacheThis method requires that you sign an assembly with a strong name. To sign an assembly with a strong name and to then install the assembly in the global assembly cache, follow these steps: Name an assembly by using a strong name. To do this, follow these steps: Use the Strong Name tool (Sn.exe) that is included with the Microsoft .NET Framework software development kit (SDK) to generate a cryptographic key pair.
At the Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Command Prompt, type the following command:
sn -k AssemblyName.snk This command generates and stores a key pair in a file that is named AssemblyName.snk.Include the following assembly-level attribute in the source code (AssemblyInfo.vb):

<Assembly: AssemblyKeyFile(“AssemblyName.snk”)>Use the Global Assembly Cache Tool (Gacutil.exe) to register the .NET Framework assembly in the global assembly cache. To do this, type the following command at the Visual Studio .NET Command Prompt:
gacutil /i AssemblyName.dllFor more information about how to work with assemblies and with the global assembly cache, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx)For more information about how to sign an assembly with a strong name, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx)Note This method may be difficult and time-consuming if many assemblies exist and if the assemblies have dependencies on Microsoft Component Object Model (COM) objects. Any assembly dependencies must also be discoverable by the CLR. Therefore, if you plan to install your assembly to the global assembly cache, also install the dependencies of the assembly into the global assembly cache.
Method 2: Use an Application.Config file together with the CodeBase tagBy using an application configuration file, you can specify the location at which the CLR should look for dependent assemblies. Specifically, use the Codebase tag. By using this tag, you can put your assemblies in a separate folder. However, this method still requires that you name the library assembly by using a strong name. To use the application configuration file, follow these steps: Compile your library assembly.Sign the assembly by using a strong name.From your main application, add a reference to your library assembly, and then compile the main application.Obtain the public key token from your strongly named library assembly. To do this, follow these steps: Click Start, click All Programs, and then click Microsoft Visual Studio .NET 2003.
Note In Visual Studio 2005, click Start, click All Programs, and then click Microsoft Visual Studio 2005.Click Visual Studio .NET Tools, and then click Visual Studio .NET 2003 Command Prompt.
Note In Visual Studio 2005, click Visual Studio 2005 Tools, and then click Visual Studio .NET 2005 Command Prompt.Change the directory to the location of your library assembly. Typically, the location is the Bin folder of your library assembly project.Type the following command, and then press ENTER:
SN -T MyLibrary.dllNote Replace MyLibrary with the name of your library assembly. To make sure that the correct value is returned, make sure that you use an uppercase T in the -T switch.This command returns a hexadecimal value that represents the token for the public key of your library assembly. Make a copy of the hexadecimal value that will be used in the application configuration file.Obtain the assembly version from your strongly named library assembly. To obtain the assembly version, use one of the following methods:Method A: Windows Explorer file propertiesIn Windows Explorer, right-click your library assembly, and then click Properties.In the Properties dialog box, click the Version tab.Under Other version Information, click Assembly Version. Make a copy of this version value that will be used in the application configuration file.Method B: MSIL Disassembler (ILDASM)At the Visual Studio .NET 2003 Command Prompt, type ILDASM, and then press ENTER.On the File menu, click Open.Click your custom assembly, and then click Open.In the bottom pane, locate the assembly version (.ver). The following is an example of the assembly version:

.assembly yourAssemblyName{.ver 1:0:1969:29451}Note In this example, the assembly version is 1.0.1969.29541.Create an application configuration file. To do this, follow these steps: Paste the following XML code in Notepad:

<configuration><runtime><assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1″><dependentAssembly><assemblyIdentity name=”MyLibrary”culture=”neutral”publicKeyToken=”fa4e781ae585ee8a”/><codeBase version=”1.0.1074.32347″ href=”FILE://C:/assemblies/myLibrary.dll”/></dependentAssembly></assemblyBinding></runtime></configuration>Change the following XML element attributes.
Collapse this tableExpand this table
Element attributeMake this changeassemblyIdentity nameChange MyLibrary to the name of your library assembly.assemblyIdentity publicKeyTokenChange the publicKeyToken attribute to the public key token value that you copied in step 4.codeBase versionChange the version attribute to the version value that you copied in step 5.codeBase hrefChange the href attribute to point to the full path name and the file name of your library assembly.Save the file as the main assembly .exe file name. Use .config as the file name extension. For example, if your main assembly is named “MyProject.exe,” the application configuration file name is “MyProject.exe.config.” You must save this file in the same folder in which your main application assembly is located.For more information, visit the following MSDN Web sites:
Specifying an assembly’s location
http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx)
<codeBase> element
http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx)
How the runtime locates assemblies
http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx)
Method 3: Use the System.Reflection.Assembly.LoadFrommethodThis method uses the Assembly.LoadFrom method to explicitly load an assembly from a fully qualified path name and from a fully qualified file name. To do this, follow these steps: Compile your library assembly.Use code that is similar to the following code example to load your assembly:

Dim yourAssembly As System.Reflection.Assembly = _System.Reflection.Assembly.LoadFrom(“c:\yourAssembly.dll”)Note If you have any dependent assemblies, you should locate them in the same path where the main assembly is located.
For more information about the Assembly.LoadFrom method, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx)
Method 4: Use the AssemblyResolve eventThe AssemblyResolve event occurs whenever the CLR unsuccessfully tries to bind to an assembly. Use the AddHandler method in the application to add an event handler that returns the correct assembly whenever the AssemblyResolve event occurs.
Note This method does not require that you name the assemblies by using strong names.
The AssemblyResolve event handler must return an [Assembly] object that represents the assembly to which the CLR must bind. Typically you can use the Assembly.LoadFrom method to load the assembly. Then, return the loaded assembly.
Note The event handler must be located in a procedure that does not contain any references to the relocated assemblies. Additionally, the event handler must be called before any code that depends on the relocated assemblies is called.
The following code is an example of an application that loads an assembly by using the AssemblyResolve event:

Module Module1Sub Main()Dim f As Form1′Set up event handler for AssemblyResolve eventAddHandler AppDomain.CurrentDomain.AssemblyResolve, _AddressOf MyResolveEventHandlerf = New Form1()Application.Run(f)End SubFunction MyResolveEventHandler(ByVal sender As Object, _ByVal args As ResolveEventArgs) As [Assembly]‘Load the assembly from the correct location and return the assembly’This handler is only called if we try to bind to the assembly and the attempt fails.Dim MyAssembly As [Assembly]MyAssembly = [Assembly].LoadFrom(“C:\assemblies\MyLibrary.dll”)Return MyAssemblyEnd FunctionEnd Module’Some sample code in Form1 which depends on the external assemblyPrivate Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim x As MyLibrary.Applicationx = New MyLibrary.Application()x.MyMethod()End SubEnd ClassFor more information about the AppDomain.AssemblyResolve event, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx)

FIX: You cannot create objects on a remote COM+ partition by using the CreateObject method when the server name and the moniker are passed as parameters

Symptoms
In Microsoft Windows Server 2003, you can use COM+ partitions to partition an application to run independently of other configurations of the application. Each configuration of the application is installed in a separate partition and can be separately managed. How you manage each configuration of an application depends on the specific needs of the application users. You can create an instance of an application that is hosted in these partitions by providing a COM+ partition moniker and a remote server name.
When you call the CreateObject method in Microsoft Visual Basic for Windows 6.0 Service Pack 5 or an earlier version, and you specify both a COM+ partition moniker and a remote server name, you receive the following error message:

Run-time error ‘463′: Class not registered on local machineNote The following syntax is used to call the CreateObject method to reproduce this problem:

‘CreateObject(“partition:{guid}/new:ProgID”,”servername”)Set p = CreateObject(“partition:{8A9B9DB1-684B-426B-BE5E-65E13F97B23A}/new:Project1.CallMe”, “REMOTESERVER”)
Resolution
Support for remote COM+ partitions is not implemented in the Microsoft Visual Basic runtime that is provided with Visual Basic for Windows 6.0 Service Pack 5 or an earlier version.

FIX: Error 800a2328 When You Browse a Visual Basic 6.0 WebClass (IIS Application)

Symptoms
When attempting to browse a WebClass application, the following error is returned:

Server object error ‘ASP 0177 : 800a2328′
Server.CreateObject Failed
/WebClassApp/WebClassFile.ASP, line 20
The operation completed successfully.

Resolution
This error occurs if Session state is disabled for the Web application.Visual Basic 6.0 WebClasses (IIS Applications) require Session state to be enabled.

FIX: Application Error When Binding Dynamically-Created Control

Symptoms
An application error occurs when a Visual Basic program dynamically createsa text box bound to a remote data control or a data control.
Resolution
Microsoft has confirmed this to be a bug in the Microsoft products listedat the beginning of this article. This bug has been corrected in VisualBasic, version 5.0.

BUG: You receive a “System.InvalidCastException” error message when you upgrade a Visual Basic 6.0 application that holds a user control to Visual Basic .NET or Visual Basic 2005

Symptoms
You upgrade a Microsoft Visual Basic 6.0 application that holds a user control to Microsoft Visual Basic .NET or Visual Basic 2005. The user control that the Visual Basic 6.0 application holds calls a custom event. When you run the Visual Basic .NET or Visual Basic 2005 application, you may receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in Project1.exe.
Additional information: Specified cast is not valid.
Resolution
This bug occurs when the index parameter determines that the code that the Visual Basic Upgrade Wizard generates does not pass the correct instance of the control array.