.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Failure to reliably iterate through all elements of a COM Collection from C# .NET 4.0

Posted By:      Posted Date: August 28, 2010    Points: 0   Category :.NET Framework
In C#, .NET 4.0, I am making a call to a COM DLL to return a collection object. A foreach loop steps through each element of the collection. In the loop, a new .NET object is created for each element of the COM collection. The new object is added to a new .NET collection. This works fine in the development environment. In a deployed system, is works correctly most (about 90%) of the time.  However, sometimes (about 5% of the time) the foreach loop finds no items in the COM collection. Other times (also about 5%), it finds the first 2 or 3 or ... N items.  Somehow, the enumeration interface exposed by the COM object is failing. Here is a code snippet from the COM Object, which is called NameList.  NOTE that the VB6 cls file was opened in a text editor to show the class and method attributes. VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "NameList" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = True Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes" Attribute VB_Ext_KEY = "Top_Level" ,"Yes" Option Explicit Private mCol As Collection ' <snip> Only code rela

View Complete Post

More Related Resource Links

How to use getElementById to get the elements in a form

There are many ways of accessing form elements, of which the easiest is by using the cross-browser W3C DOM document.getElementById method. Before we learn more about this method, it would be useful to know something about the Document Object Model (DOM), the concept of HTML nodes or elements, and the concept of containers.

Each time you load an HTML page, the web browser generates an internal representation of the page in the form of an inverted tree structure. Let us look at a simple form. We will use this form later to demonstrate the use of the getElementById method.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)


Hey guys

I came across this bug using while trying to implement a sitemap

Line 147:						Panel panel = new Panel();
Line 148:						panel.Controls.Add(_viewState);
Line 149:						Page.Form.Controls.Add(panel);
Line 150:						string script = "document.getElementById('" + _viewState.ClientID + "').value = GetViewState__AspNetTreeView('" + Extender.MakeChildId("UL") + "');";
Line 151:						Page.ClientScript.RegisterOnSubmitStatement(typeof(TreeViewAdapter), _viewState.ClientID, script);

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +8696951
   CSSFriendly.TreeViewAdapter.SaveAdapterViewState() in E:\wwwroot\ThirdParty\CSSFriendly\CSSFriendly_24242\CSSFriendly\TreeViewAdapter.cs:149
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +8901848
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148
   System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +148

Basic Instincts: Collection and Array Initializers In Visual Basic 2010


Collection Initializers are a great addition to the language and allows concise syntax in order to initialize both framework and user defined collection types.

Spotty Bowles

MSDN Magazine October 2009

Transactions: Build Scalable Systems That Handle Failure Without Losing Data


Systems that handle failure without losing data are elusive. Learn how to achieve systems that are both scalable and robust.

Udi Dahan

MSDN Magazine July 2008

Security: Manipulate Privileges in Managed Code Reliably, Securely, and Efficiently


When the author was faced with implementing support for changing a security descriptor on an object, he noticed there was not support for that operation in .NET. So he devised two solutions to the problem: the first, simpler one, is tailored to the .NET Framework 1.1 and can be used today. The second solution incorporates several advanced features available only in the .NET Framework 2.0. Both are presented here.

Mark Novak

MSDN Magazine March 2005

Command Management: Use Design Patterns to Simplify the Relationship Between Menus and Form Elements


In Windows Forms applications, similar commands, such as those in a menu and their counterparts on a toolbar, are not automatically related. They don't fire the same event or run the same handler routine. Yet code that allows the same or similar user commands to fire the same code simplifies development.This article describes the principles of command management and why it's important to have functional commands that are not exclusive to any one UI element. In order to provide an MFC-like command management infrastructure, a design pattern is developed and applied as a series of C# classes.

Michael Foster and Gilberto Araya

MSDN Magazine October 2002

Garbage Collection-Part 2: Automatic Memory Management in the Microsoft .NET Framework


The first part of this two-part article explained how the garbage collection algorithm works, how resources can clean up properly when the garbage collector decides to free a resource's memory, and how to force an object to clean up when it is freed. The conclusion of this series explains strong and weak object references that help to manage memory for large objects, as well as object generations and how they improve performance. In addition, the use of methods and properties for controlling garbage collection, resources for monitoring collection performance, and garbage collection for multithreaded applications are covered.

Jeffrey Richter

MSDN Magazine December 2000

Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework


Garbage collection in the Microsoft .NET common language runtime environment completely absolves the developer from tracking memory usage and knowing when to free memory. However, you'll want to understand how it works. Part 1 of this two-part article on .NET garbage collection explains how resources are allocated and managed, then gives a detailed step-by-step description of how the garbage collection algorithm works. Also discussed are the way resources can clean up properly when the garbage collector decides to free a resource's memory and how to force an object to clean up when it is freed.

Jeffrey Richter

MSDN Magazine November 2000

How to Cache System.Collection.Generic.List object


I need to cache System.Collection.Generic.List object with expiration time

var Root = from feed in feedhotel.Descendants(ota + "HotelContent")
                           where (feed.Attribute("HotelCode") != null)
                           select new xmlhotel()




return Root.ToList();

Is it possible to cache Root.ToList() for some time....

Customize failure text for login control


Hi all,

I would like to show the failure text in a client-side alert box for login control in c#. Please help me if there is an easy way to convert it from vb.net to c#.net. Thanks a lot!

Protected Sub Login1_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoginError
'Display the failure message in a client-side alert box
ClientScript.RegisterStartupScript(Me.GetType(), "LoginError", _
String.Format("alert('{0}');", Login1.FailureText.Replace("'", "\'")), True)

 End Sub

 Converted, but it doesn't work.

protected void Login1_LoginError(object sender, System.EventArgs e)
    //Display the failure message in a client-side alert box
    ClientScript.RegisterStartupScript(this.GetType(), "LoginError", string.Format("alert('{0}');", Login1.FailureText.Replace("'", "\\'")), true);

Finding a control from a control collection



I'm trying to write a bit of code that will format all the numeric values in lables in my gridview. here's what i am doing right now:

On the RowDataBound Event, i create a row object for the row that is currently being bound.
then i go use the FindControl method to grab each label by name.

Problem is there's too many columns in this table and i really dont wnat to have to type it all out. Here's an example of what i am currently doing

' Get row being bound
Dim myRow = e.Row

' Get labes to be formatted
Dim lblMunicipalIncineration = CType(myRow.FindControl("lblMunicipalIncineration"), Label)

' format labels
If lblMunicipalIncineration IsNot Nothing Then
    If Not String.IsNullOrEmpty(lblMunicipalIncineration.Text) Then
        lblMunicipalIncineration.Text = FormatNumber(lblMunicipalIncineration.Text, 0, TriState.False, TriState.False, TriState.True)
    End If
End If

Can i use myRow.Controls to grab a list of all the controls and then somehow get a list of all labels in the collection and then use a for loop to go through and format each label?

Multiple services in REST Collection WCF Service?


After I create a "REST Collection WCF Service" project, it contains one service "service.svc". Can I add multiple services to this project? There must be a way to do so. Otherwise, it does not make sense to create one project for each service.


My question is how to add a new service to an existing "REST Collection WCF Service" project?


Thanks a lot.

How to get access to content database after server hardware failure


Sharepoint server 2007 and remote sql 2000 SP4 database, after ShraPoint server crash only content databases are available and intact. Trying to restore from backup fail, stsadm -o restore says that not valid backup is available on path. How to get access to available content database using a new installation with same server name, ip address and partitions configuration. How to recover site with content databases information.

Regards, thanks for help.

Victor Naranjo MCSE + Security MCSA + Security MCSE + Messaging MCSA + Messaging ITIL Certified Comptia Security+
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend