SQL Server 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 ‘control’

PRB: Choosing Quit in WFC Exception Dialog Box Shuts Down Host

Symptoms
When an ActiveX control created with Windows Foundation Classes for Java(WFC) is placed in Visual Basic and throws an exception, the default WFCexception dialog box gives you the choice to either Continue, Quit, or SeeDetails. If you click Quit, the entire process hosting the WFC controlquits.
In an application hosted by the Visual J++ Integrated DevelopmentEnvironment (IDE), this is okay because the application is running in adifferent process than the IDE. However, in Visual Basic, the IDE and theapplication are in the same process until you actually make an .exe, sochoosing Quit kills the entire Visual Basic IDE, along with any unsavedwork.
Resolution
There are two methods for resolving this issue. The first method is tonever choose Quit in the WFC exception dialog box that appears when youreceive an unhandled exception while hosting a WFC control in a VisualBasic application while running in the Visual Basic IDE. Instead, youshould always choose Continue and then shut down the Visual Basicapplication from within the Visual Basic IDE.
The second method is to add an onThreadExceptionEventHandler to your WFCActiveX control. This event handler is called instead of the default WFCexception handling routines and the default dialog box that contains theQuit option does not appear. Your WFC control, however, is responsible forhandling exceptions, notifying the user, and shutting down as necessary.
Sample CodeThe following sample code adds a very simple ThreadException Handler to aWFC ActiveX control:

// … Other WFC imports …import com.ms.wfc.app.*; // This creates the Application object.public class Control1 extends UserControl {// Define a Thread Exception Handler Delegate.private void threadException(Object sender, ThreadExceptionEvent e) {MessageBox.show(“In Exception Handler”);}public Control1() {// Add the Thread Exception Handler delegate.Application.addOnThreadException( new ThreadExceptionEventHandler(this.threadException));// Required for Visual J++ Form Designer support.initForm();}// The rest of your WFC ActiveX control goes here… (c) Microsoft Corporation 1998, All Rights Reserved. Contributions by SteveHorne, Microsoft Corporation.

InvalidCastException when you bind DateTimePicker that contains a null value in Visual Basic

Symptoms
When you bind a DateTimePicker control to a data source, you may receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in mscorlib.dll
Additional information: Object cannot be cast from DBNull to other types.
Resolution
This behavior occurs if the field that is bound to the DateTimePicker control contains a null value that is represented by the System.DBNull object. The DateTimePicker control does not support the System.DBNull object.

“License cannot be granted” exception when you add a licensed user control to the Windows Form in the same solution

Symptoms
When you create a licensed user control in a project, and then add it to the Windows form in the same solution, you receive the following exception:

An exception occurred while trying to create an instance of ‘Projectname.Controlname’. The exception was “An instance of type ‘Projectname.Controlname’ was being created, and a valid license could not be granted for the type ‘Projectname.Controlname’. Please, contact the manufacturer of the component for more information.”However, when you create a new solution, and then add the licensed user control to the Windows form, the licensed user control is added.
Resolution
Any user control that is implicitly added to the toolbox refers the assembly file in the obj\Debug folder. Therefore, the licensed user control that is created refers to the WindowsControlLibrary1.dll file in the obj\Debug folder. Generally, the license (.lic) files are put in the Bin folder. When you add an instance of a licensed user control to the Windows form in the same solution, the license is validated. Because the .lic file is not located in the obj\Debug folder, you receive the exception.