.NET Questions and Solutions

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

FIX: A System.ArgumentException exception occurs when you try to create a Windows form that inherits from a Windows form that contains a dataset

Symptoms
When you try to create a Windows form that inherits from a Windows form that contains a dataset in Microsoft Visual Studio .NET 2003, you may receive the following exception error message:

An unhandled exception of type ‘System.ArgumentException’ occurred in system.data.dll
Additional information: Cannot add primary key constraint since primary key is already set for the table.
Resolution
If the dataset contains nested datatables, Visual Studio .NET incorrectly tries to add additional code to the inherited Windows form to enforce constraints on the inherited dataset. This code tries to add a constraint that already exists on the dataset. Therefore, the behavior that is mentioned in the “Symptoms” section occurs.

Description of the Forms Server 2007 hotfix package (Ifswfe-x-none.msp): October 27, 2009

Symptoms
This article describes the Microsoft Office Forms Server 2007 issues that are fixed in the hotfix package that is dated October 27, 2009.
Resolution
Issues that this hotfix package fixesYou try to fill a Microsoft Office InfoPath 2007 form that is enabled for display on a mobile device. If the form contains a drop-down list that is bound to a Windows SharePoint list, a NullReferenceException exception occurs.
You use Forms Server 2007 to render an HTML form that contains an Office InfoPath 2007 template. The template is contained in a section of the form. When you use Mozilla Firefox to scroll through the section, the Cannot be blank red asterisks do not scroll together withother items in the section.

BUG: Report Preview with Form’s Name Clears Object Reference

Symptoms
Issuing a REPORT FORM PREVIEW command on a report that has the same filename as a form that is running causes the form to lose its objectreference. If you attempt to reference the form object in code, this errormessage appears:

Object not found
Resolution
If you have a form called Customer and issue the following command, anobject reference named Customer is created for the form:

DO FORM Customer Preview a report named Customer by issuing the following command:

REPORT FORM Customer PREVIEW The customer object now references the Report Preview window. When theReport Preview window is closed, the customer object is released. Workaround this problem by giving your forms a different object reference thanthe reports with the same name. Do this by using the NAME clause in the DOFORM command. For example, to give the Customer Form an object reference ofCustform, you would issue the following command:

DO FORM Customer NAME Custform

BUG: Assignment of Multi-Valued Objects in Request Object Causes Corruption in Scripting Dictionary

Symptoms
If items in the Request.Form or Request.QueryString collections that contain multiple values are assigned to a Scripting Dictionary object stored in the Session object, data corruption may occur. Corrupted data will manifest either as empty fields (that is, blank individual dictionary items) or items that contain garbage text.
A common example of this is a checkbox; checkbox controls can have the same name so that multiple values of a single named item can be returned to the server.
Resolution
When the assignment of a multi-valued item in the Request.QueryString or Request.Form collections to an item in the Scripting Dictionary object takes place without explicitly using the Item property of the object within the collection, a reference to the actual object is stored in the Scripting Dictionary instead of the intended value. This means that when the item stored in the Scripting Dictionary is referenced again, the Scripting Dictionary will refrence an object in the Request.QueryString or Request.Form collections instead of the intended string or integer values.

ACC2000: How to Use Collections to Manage Class Objects in VBA

Symptoms
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you how to use collections in Visual Basic forApplications to manage references to class objects in Access 2000.This technique allows your class objects to persist, and enables you tocontrol the individual properties of those objects by using the familiarcollection syntax used in Microsoft Access for implementing Data AccessObjects (DAO) and other Microsoft Office Object models.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Resolution
In order to use collections to manage class objects, you must do thefollowing:Create an instance of the classSet the properties and methods of the classAdd the class to a public collectionUnload the instance of the classYou might expect that unloading the instance of the class results in theclass being closed and terminated. However, the class object persistsbecause you add it to a collection, which then owns the reference to theclass. This is a very powerful technique that allows you to control objectreferences through a collection; the class object does not terminate untilyou remove it from the collection.
The following example creates a class object and a form object, and thenmanages both objects from a collection in a standard module.
Create a Class ModuleCreate a new database called ClassTest.mdb.On the Insert menu, click Class Module.Save the class module as clsTest.Type the following lines in the Declarations section:

Private This_ClassID As StringPrivate This_frm As New Form_frmTest Select Class in the Object box of the Module window. “Initialize” isautomatically selected in the Procedure box.Type the following procedure:

Private Sub Class_Initialize()On Local Error GoTo Class_Initialize_ErrDim Msg As StringThis_frm.Visible = TrueThis_ClassID = “Initialized”This_frm.Caption = This_ClassIDMsgBox “Class Initialized”, vbInformation, “Class Example”Class_Initialize_End:Exit SubClass_Initialize_Err:Msg = “Error #: ” & Format$(Err.Number) & vbCrLfMsg = Msg & Err.DescriptionErr.Raise vbObjectError, “clsTest.Initialize (Private)”, MsgResume Class_Initialize_EndEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures

Public Property Get ClassID() As VariantClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As Variant)This_ClassID = vNewValueThis_frm.ClassID = This_ClassIDThis_frm.Caption = This_ClassIDEnd Property Save and close the clsTest class module.
Create a FormCreate the following form not based on any table or query in Designview:

Form: frmTest—————–Caption: TestForm With the form still open in Design view, click Code on the View menu.Type the following line in the Declarations section of the form’s classmodule:

Dim This_ClassID As String Add the following event procedure to the form’s Unload property:

Private Sub Form_Unload(Cancel As Integer)col.Remove This_ClassIDEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures. Notethat the Get ClassID() function and vNewValue variable in this exampleare dimensioned as String instead of the default, which is Variant:

Public Property Get ClassID() As StringClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As String)This_ClassID = vNewValueEnd Property Save and close the frmTest form.
Create a Standard ModuleCreate a new standard module and save it as Module1.Type the following line in the Declarations section:

Public col As New Collection Type the following procedure:

Function CreateClassTest() As String’ Create an instance of the clsTest class module, which creates’ an instance of the frmTest form.Dim cls As New clsTest’ Create a unique identifier string and set it to the upper index’ of the Public col Collection plus one.Dim varClassId As StringvarClassId = “Key_” & CStr(col.Count + 1)’ Set the clsTest class module’s ClassID property to the value of’ varClassId, which in turn sets the frmTest.ClassId property to’ the same value. This is so the form has a method to track its’ relationship to the collection.cls.ClassID = varClassId’ Add the instance of the class object to the collection passing’ varClassId as the Key argument.col.Add cls, varClassIdMsgBox “Created New Collection Item: ” & varClassId, _vbInformation, “Class Example”‘ Unload the cls object variable.Set cls = Nothing’ Return the varClassId.CreateClassTest = varClassIdEnd Function Close and save the module.
Test the ExampleWhen you call the CreateClassTest() function multiple times, it opensmultiple instances of the frmTest form, each of which is unique and capableof managing itself and its participation in the public collection. Eachform is aware of its Key position in the collection, and each one removesitself from the collection when you close the form.
The following sample procedure creates three instances of the clsTestclass:Create a standard module and type the following procedure:

Function CreateThreeItems() As BooleanDim strKeys(1 To 3) As StringDim i As IntegerFor i = LBound(strKeys) To UBound(strKeys)strKeys(i) = CreateClassTest()Next iFor i = LBound(strKeys) To UBound(strKeys)MsgBox col.Item(strKeys(i)).ClassID, vbInformation, _”Class Test”Next iEnd Function To test this function, type the following line in the Immediate window,and then press ENTER:

?CreateThreeItems() Note that messages boxes are displayed each time the clsTest classmodule initializes, when each of three instances of the frmTest form iscreated, and again after all three instances of the form are open.