Microsoft KB Archive/814342

= &quot;License cannot be granted&quot; exception when you add a licensed user control to the Windows Form in the same solution =

Article ID: 814342

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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 &quot;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.&quot;

However, when you create a new solution, and then add the licensed user control to the Windows form, the licensed user control is added.



CAUSE
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.



RESOLUTION
To resolve this problem, move the .lic file to the obj\Debug folder. Follow these steps to resolve the problem described in the &quot;More Information&quot; section of this article:
 * 1) On the Project menu, click Show All Files.
 * 2) In Solution Explorer, open the bin folder.
 * 3) Right-click WindowsControlLibrary1.UserControl1.lic, and then click Copy.
 * 4) Expand obj, right-click Debug, and then click Paste.
 * 5) From the toolbox, drag UserControl1 to Form1.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows Control Library project by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

By default, UserControl1 is created. On the View menu, click Code.  Replace the existing code with the following code:

Visual Basic .NET or Visual Basic 2005 Code Imports System.ComponentModel 'Create a license on the User Control.  Public Class UserControl1 Inherits System.Windows.Forms.UserControl

Private license As license Public Sub New
 * 1) Region &quot; Windows Form Designer generated code &quot;

MyBase.New ' Validate whether or not the License can be granted. license = LicenseManager.Validate(GetType(UserControl1), Me) 'The Windows Form Designer requires this call. InitializeComponent

End Sub

'UserControl1 overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If Not (license Is Nothing) Then license.Dispose license = Nothing End If

If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing)

End Sub

'Required by Windows Form designer. Private components As System.ComponentModel.IContainer  Private Sub InitializeComponent

components = New System.ComponentModel.Container

End Sub

End Class
 * 1) End Region

Visual C# .NET or Visual C# 2005 Code using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms;

namespace WindowsControlLibrary1 {   //Create the license on the User Control. [LicenseProviderAttribute(typeof(LicFileLicenseProvider))] public class UserControl1 : System.Windows.Forms.UserControl {       private System.ComponentModel.Container components = null; private License license = null;

public UserControl1 {           // This call is required by the Windows.Forms Form Designer. InitializeComponent; // Validate whether or not the license can be granted. license = LicenseManager.Validate(typeof(UserControl1), this); }          protected override void Dispose( bool disposing ) {           // Destroy the license. if (license != null) {               license.Dispose; license = null; }           // Clean up any resources that are being used. if( disposing ) {               if( components != null ) components.Dispose; }           base.Dispose( disposing ); }

#region Component Designer generated code private void InitializeComponent {           components = new System.ComponentModel.Container; }       #endregion } }  Locate the bin folder in WindowsControlLibrary1 by following the corresponding path on your computer. Open the Debug folder in the bin folder.</li> In the Debug folder, create a new WindowsControlLibrary1.UserControl1.lic text document.</li> In the Rename dialog box, click Yes.</li>  Open the WindowsControlLibrary1.UserControl1.lic file in Notepad, and then paste the following code: WindowsControlLibrary1.UserControl1 is a licensed component. </li> Save the file, and then close it.</li> On the Build menu, click Build Solution.</li> On the File menu, point to Add Project, and then click New Project.</li> Under Project Types, click Visual Basic Projects or Visual C# Projects.</li> Under Templates, click Windows Application.

By default, Form1 is created.</li> From the toolbox, drag the UserControl1 to Form1.

You may receive the exception described in the &quot;Symptoms&quot; section of this article.</li></ol>

Note This problem occurs only when you use the Microsoft .NET Framework default license provider LicFileLiscenseProvider. When you write your own custom license providers, this problem does not occur.

<div class="references_section">