A scenario where LINQ is too dynamic (answered)

The situation I talked about in my previous post had one very big issue: LINQ sometimes just is too dynamic… ;)
The fact that I changed the value for the Ranking property for each SpecificObject that got matched, made the primary LINQ query have a different outcome. LINQ uses lazy evaluation and  because of that, the contents of finalGroups and exitGroups changed every time I changed the Ranking value for one of the SpecificObjects. Each time the variable containing the outcome of the LINQ query is iterated through, the query is evaluated again. This made that the matching was all off, and I ended up with a bunch of exit groups that got matched!

To have a simple solution, I did the following. This holds the final and exit objects in a ‘static’ list in stead of a dynamic query result. This way, the SpecificObjects in the finalObjects and exitObjects lists stay the same, regardless of the changes you make to any of the SpecificObjects.

int count;
int numberOfPossibilities;
IEnumerable<SpecificObject> temp;
List<SpecificObject> finalObjects;
List<SpecificObject> exitObjects;

count = 0;
finalObjects = new List<SpecificObject>();
exitObjects = new List<SpecificObject>();

temp = from SpecificObject specificObject in AllSpecificObjects
       orderby specificObject.Ranking
       select specificObject;

foreach (SpecificObject specificObject in temp)
{
    if (count < numberOfPossibilities)
    {
        finalObjects.Add(specificObject);
    }
    else
    {
        exitObjects.Add(specificObject);
    }
    count++;
}

By the way, we have a winner. Ruud Campsteijn was pretty quick with the right answer!

A scenario where LINQ is too dynamic

While developing an algorithm to match preferences to possibilities, I had to sort a generic list of a specific object type (SpecificObject). The first x objects would be matched, the rest would be excluded because of the number of available places. To determine the group of objects that would be matched and the group that would be excluded, I did something like this:

IEnumerable<SpecificObject> sortedObjects;
IEnumerable<SpecificObject> finalObjects;
IEnumerable<SpecificObject> exitObjects;

sortedObjects = from SpecificObject specificObject in AllSpecificObjects
                orderby specificObject.Ranking
                select specificObject;

finalObjects = sortedObjects.Take<SpecificObject>(numberOfPossibilities);
exitObjects = sortedObjects.Skip<SpecificObject>(numberOfPossibilities);

Next I would iterate through the possibilities and look for a match in (a subset of) the ‘finalObjects’ list. To be sure the matching would be done honestly during this process, I changed the value for the Ranking property after a positive match. This way, SpecificObjects that had no match yet would move up in the ranking and would be matched before SpecificObjects that already had one or more positive matches. Unfortunately, this had a very unwanted side-effect.

Can you think of it? Let me know and put it in the comments. The answer can be found in my next post.

By |June 6th, 2008|.Net, Linq|1 Comment

Unable to connect publishing custom string handler …

After developing a custom Web Service to be hosted in SharePoint (based on this HowTo article on MSDN) and deploying it on a testing environment, I got some entries in the EventLog over there, stating:

Unable to connect publishing custom string handler for output caching. IIS Instance Id is ‘xxxxxxxxx’, url is ‘http://internalsharepointserver/somesubfolder/services.asmx

The PublishingHttpModule trying to cache an unmanaged path seems to be the problem here. When enabled for a custom web application (such as my web service), it causes the eventlog entry each time certain requests like web service calls or static CSS files are made.

The quick solution is to simply remove the PublishingHttpModule for the custom web application. You can do so by editing the web.config file:
<httpModules>
    <remove name=PublishingHttpModule/>
    …
</httpModules>

To completely switch back to all default ASP.NET modules, use these settings in the web.config file:
<httpModules>
    <clear/>
    <add name=OutputCachetype=System.Web.Caching.OutputCacheModule/>
    <
add name=Sessiontype=System.Web.SessionState.SessionStateModule/>
    <add name=WindowsAuthenticationtype=System.Web.Security.WindowsAuthenticationModule/>
    <add name=FormsAuthenticationtype=System.Web.Security.FormsAuthenticationModule/>
    <add name=PassportAuthenticationtype=System.Web.Security.PassportAuthenticationModule/>
    <
add name=UrlAuthorizationtype=System.Web.Security.UrlAuthorizationModule/>
    <add name=FileAuthorizationtype=System.Web.Security.FileAuthorizationModule/> 
    …
</httpModules>

The number of fractional digits is out of range?

After extending a textbox with a NumberUpDownExtender, which comes with the AJAX Control Toolkit, I recieved the error “the number of fractional digits is out of range”. This happened when debugging my web application and using the extender. When using the web app in non-debug mode, nothing happened. This included the value change ‘not happening’.

Fortunately, this one was REALY easy, but it is not to be found on the web. That’s why I am posting this embarassing personal slip-up anyway. As it turned out, I switched the values in minimum and maximum. Make sure the Maximum property of the extender holds the highest value of the two and you should be good to go ;)

By the way, you would think something like this a) could be determined design- or compiletime, and b) could generate a better exception than ‘number of fractional digits out of range’.

Error connecting to undo manager

 vs_errorWhile developing a web application for a friend of mine, I ran into a Visual Studio error message I hadn’t seen before: Error connecting to undo manager of source file ‘D:DevelopmentSomeWeb ApplicationDefault.master.designer.cs’.

The error occured whenever I tried to start the (web) application in debug mode. The application did start OK after the error message though. Starting the application without debugging didn’t cause the error.
I closed the solution file I had opened, but that didn’t solve the  issue. Stopping the ASP.NET Development Server didn’t do anything either. I had to exit Visual Studio al together to get this error to go away.

I searched for this specific error, and saw it already occured in Visual Studio 2005 SP1. Scott Hanselman has a blog post on how he solved this issue. Now, in Visual Studio 2008, this error still seems to be in there…

Rick van den Bosch .net – part II

It’s been a while because I’ve been so busy lately, but the next (technical) step for my personal  website is now online. When browsing to http://rickvandenbosch.net/blog/, the content of my blog here is displayed sort of mirrored over there. I used an OpenSource RSS component to read my private rss feed from blogging about, and display it from there.

There’s still a lot of work to be done (like styling!), but I’ll get there. Eventually…. ;)

If you’re interested on the how and the what: I’ll post the source code online as soon as the entire website is ‘ready’. At this rate, it might take a while, so if you can’t wait: contact me

Technorati tags: ,,,

Selecting all the controls of a specific type, the (built in!) LINQ way

We probably all worked with dynamically generated controls on forms, ASP.NET pages or (user) controls. And I guess we’ve all written for-each statements to loop through the ControlCollection and filter out all the controls of a specific type, right? I wanted to do the same today, so I started by trying to use LINQ for this:
linq_OfType_1 
This, as you can see, generated a compile time error:
Could not find an implementation of the query pattern for source type ‘System.Web.UI.ControlCollection’.  ‘Where’ not found.  Consider explicitly specifying the type of the range variable ‘control’.

As the error states, this is easily solved by explicitly specifying the type of the control variable. Because a ControlCollection can contain so many types, it is not strange the compiler asks you to be a bit more precise on the type you will be querying. That looks something like this:
linq_OfType_2

But when I added the System.Linq namespace to my using section, I saw an extra method on the ControlCollection, called OfType<>. This method is one of the extension methods that can be applied to a collection that has a non-parameterized type like a ControlCollection or an ArrayList, because OfType<> extends the type IEnumerable.
Another one of those nice ‘native’ Linq extension methods is the Cast<> method, which pretty much does the same but throws an exception if there are any objects in the collection that cannot be cast to the specified type. And yes, exceptions are sometimes wanted ;)

For more info on the OfType<> method, see: Enumerable.OfType<TResult> Generic Method
For more info on the Cast<> method, see: Enumerable.Cast<TResult> Generic Method

Technorati tags: ,,

‘EntLib Extensions To WSSF’ released to CodePlex

Some of my (Dutch!) Avanade colleagues recently released ‘Entlib Extensions To WSSF’ to CodePlex. Quite a cool addition to the already rich Enterprise Library.



Entlib Extensions to WSSF (Web Service Software Factory) is an extension that enables WSSF to easily integrate with Enterprise Library. This extension allows you to very easily handle cross cutting concerns in WCF services like Validation, Exception Handling and Logging without much programming. The extension accomplishes this by using the policy injection application block of enterprise library.


Have a look at it over at the CodePlex page: EntLib Extensions To WSSF

Other info:
Web Service Software Factory
Enterprise Library 3.1


LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

When you dynamically generate a LinkButton in code that will be placed inside an UpdatePanel, you’ll experience that the Click event for the LinkButton results in a full postback in stead of the UpdatePanel being triggered. There’s a rather simpel solution to this problem…

First, let’s take a look at the scenario. Consider this HTML:
HTML_markup
There’s a literal control outside of the UpdatePanel. There’s a second literal control inside the UpdatePanel. Besides that, there is a PlaceHolder inside the UpdatePanel, where the generated LinkButton will be placed. The parent control doesn’t have to be a PlaceHolder, it can also be a repeater or something like that.

Next, consider this code:
linkbutton_code
As you can see, a LinkButton is generated and added to the PlaceHolder in every PageLoad. In the same PageLoad, the literal outside of the UpdatePanel is updated. The literal inside the UpdatePanel is only updated when the LinkButton is clicked. This makes for something like this for the first time the page loads:
first_load 
and something like this after the LinkButton is clicked:
lbclicked_load 
Because the LinkButton is inside the UpdatePanel, you would think hitting it would trigger the UpdatePanel so only the literal control inside the UpdatePanel gets updated. The fact that the two literals have the same time on it in the ‘after LinkButton clicked’ image, shows that the entire page was posted back.
The problem here is in the fact that the ID for the LinkButton is not set in the code. Weird thing is that when you change the code to generate a normal ASP.NET Button (with or without an ID), the problem doesn’t occur. This has something to do with the fact that both the input rendered for the button and the link rendered for the linkbutton have no ID, but the input control does have a name that can be used to relate events/postbacks to. The link doesn’t have that either, so I’m guessing it has something to do with that.

So the solution is rather simple. Add a line in code where the ID for the control is set (like linkButton.ID = “TestLinkButton”;) After clicking the linkbutton in my example, only the literal in the UpdatePanel is updated. Hooray! ;)
lbclicked2_load 


kick it on DotNetKicks.com


Technorati tags: ,,

Error adding an OleDb Data Connection to Visual Studio Server Explorer

The error I’m getting when setting up an OleDB connection to an Access database* via the Server Explorer in Visual Studio 2008 is Incorrect Add Connection screen‘Format of the initialization string does not conform to specification starting at index 0’. The screen doesn’t display the way it should be displayed, as you can see in the screenshot on the left.

There is a possible fix at svenM’s blog (it’s about checking the existence of a key in the registry called ‘ProgID’ and the default value, as you can read in this article), but this doesn’t seem to work for my situation.
It took me some time trying lots of  ‘possible fixes’ (like the registry change, attempts to (re-)install mdac, uninstalling and installing Visual Studio, and so on) . None of them helped. I even went through all four pages of this lengthy thread, but to no avail. The only thing I haven’t tried yet is the most rigorous solution: format the disk and start with a fresh install.

I’m getting annoyed over here…, if you have any tips: let me know.

* I won’t go in to whether an Access file is or is not a database ;). And although I generally wouldn’t consider Access as the database solution for an important business application, I do think it does have its pro’s in specific scenarios, like for a relative small web application.