Friday, May 30, 2008

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

This annoying error appeared out of nowhere in a project that used to work. I thought that the fault was of the new AjaxControlToolKit and the way it tries to register a CSS file. I was about to make really complex and , apparently, useless changes to my code until I've stumbled upon Rick Strahl's blog. It is not in the entry, but further down in the comments, but it's there.

Here is a distilled version: the error is not a page error, but a naming container control error. That means that if you do this in a PlaceHolder, let's say, it will only throw an error if the PlaceHolder has code blocks. The same applies to the header! The problem was not AjaxControlToolKit, but my own modifications to the MasterPage, where I had added script blocks with a dynamic URL.

So I added the script tags from the codebehind using this:

private void AddHeaderScript(string src)
HtmlGenericControl hgc=new HtmlGenericControl("script");
hgc.Attributes.Add("type", "text/javascript");
hgc.Attributes.Add("language", "JavaScript");
hgc.Attributes.Add("src", src);

. I don't know how this would work during an Ajax postback, but I wasn't interested in this, being a MasterPage thing. And it worked.

So, next time you get this error, be sure you know what control generated it. It can be solved by putting an extra PlaceHolder or, as is this case, replacing just some specific few code blocks.

Thursday, May 29, 2008

Text searching in .NET, not as I expected.

Update: the title of the post should have been "Text searching in general, not as I expected". I did a quick test in javascript. Do 1000 indexOf and then 1000 search(RegExp object) in a long text and measure time. Here are the results:
Internet Explorer 7: search 90% of indexOf time!
FireFox: search 355% of indexOf time!?!
Chrome: search and indexOf same speed.

Update February 2016: I've tried it again, with Firefox 44.0.2, Chrome 48.0.2564.109 m and Internet Explorer 11.0.9600.18163 and indexOf is marginally faster than regex for all of them. For the case insensitive search, Firefox and Chrome take 50% than indexOf, while Internet Explorer is not only the fastest, but also only adds 4% when searching case insensitive.

It makes sense to have the same implementation of indexOf and regular expressions as in .Net for the Microsoft browser, so I am not that surprised, but what happened with FireFox? My hypothesis is that they used Boyer-Moore in indexOf (as any decent programmer should have!) yet used a simpler implementation for their regular expression engine.

A few days ago I was preaching that IndexOf with StringComparison.Ordinal is the fastest thing for text searching. I was even implying that it uses the Boyer-Moore algorithm from a Windows system library written in c++. I was dead wrong!

Thanks to Meaflux who told me that the Internet said things differently, I delved even further into the code, past the .NET code and into the library code of mscorwks.dll where the IndexOfString method that ordinal IndexOf uses, written in c++, appears to be a pathetic linear algorithm!!

Even worse, the Boyer-Moore algorithm is used in the .NET framework in the Regex class! The Regex class is written entirely in managed code, even if it uses all sorts of cool hacks. I've also tried a Boyer-Moore implementation from Codeproject that also included the Apostolico-Giancarlo, BCL, Horspool and Turbo Boyer Moore algorithms. IndexOf was slower!

Anyway, the code is simple: take a one megabyte Harry Potter text book and search a 100 byte text from somewhere near the middle of the text. Use IndexOf with StringComparison.Ordinal, then Regex with both variants, compiled and ad-hoc creation, then the class from CodeProject.

    Results for 100 tries:
  • Ordinal IndexOf - 390 milliseconds
  • Normal IndexOf - 2380 milliseconds (6 times slower!!)
  • AdHoc Regex - 190 milliseconds (take into acount the creation of the regex 100 times plus the use of Regex.Escape 100 times on the search pattern)
  • Compiled Regex - 160 milliseconds
  • Codeproject Boyer-Moore - 230 milliseconds

Would you have believed this?

Maybe the algorithm was better because the search string was 100 bytes, so I made it 12 bytes and redid the test:

    Results for 100 tries:
  • IndexOf - 440 milliseconds(more than with 100 bytes!)
  • Normal IndexOf - 2410 milliseconds!!
  • AdHoc Regex - 235 milliseconds
  • Compiled Regex - 230 milliseconds
  • Codeproject Boyer-Moore - 368 milliseconds (a lot more, similar to IndexOf, yet still less)

Ok, maybe the problem was that the string in which I searched was big. Let's make it 5000 bytes!

    Results for 100000 (1000 times more operations):
  • Ordinal IndexOf - 1109 milliseconds
  • Normal IndexOf - 1109 milliseconds
  • AdHoc Regex - 2500 milliseconds
  • Compiled Regex - 375 milliseconds!!!
  • Codeproject Boyer-Moore - 1312 milliseconds (finally something slower than IndexOf)

So Regex appears to be best for string searching in most cases!

Update: after optimizing the Boyer-Moore class and adding my own goodness to it, I made more tests. The Boyer-Moore algorithm can be improved by adding more preprocessing, therefore it is more and more efficient as the pattern size increases and the number of pattern searches decreases. In those cases (pattern length of a few thousand characters) Regex is not so good, especially if one has to Escape the entire sequence and instantiate the class on each search. Anything over 500 characters in search pattern length made Regex perform less than the most preprocessing BM I've used.

Blake's 7!

Update 24 Aug 2013: SyFy rejected the Blake's 7 idea. That may be a good thing, though, as it apparently was taken by Microsoft, to be made as an XBox series.

Update: The previous attempts to revive Blake's 7 have failed, but a new venture to do so was announced on July 23 2012: Martin Campbell And Georgeville TV Shop Reboot Of Cult U.K. Sci-Fi Series ‘Blake’s 7′. I just hope they don't screw it up.

When I was a very young boy, during the Romanian communist era, the only entertainment available was the Bulgarian television (also a communist country, but with a more relaxed regime) who's signal would reach Bucharest to the delight of many. I have always remembered vaguely a British series called Blake's 7, a sci-fi show that I've enjoyed tremendously at the time. Recently I was reminded of it and I was lucky enough to find the torrent for all four seasons. Having watched it now, I have mixed feelings and a new understanding of my child mind.

A short description of the show first. Imagine a team of space wanderers a little in the style of Farscape's crew (civilians, each one with their own ideas and motivations), stuck in a universe that resembles the Star Wars universe (an oppressive Federation ruling the galaxy with an iron fist) and has similar effects and inspiration as Star Trek TOS. All this with a budget that was probably several levels of magnitude smaller than that of ST TOS and also with effects and script a whole lot cheesier (and by that I mean that if I work out the percentages, more than half of the show was just cheese). The actors themselves were British and Welsh TV theater actors and they behaved as such the whole series. Not that it wasn't a refreshing perspective, even now. It was actually original enough and if it weren't for the production values, it might have been a world class classic.

Of course, I didn't watch it now because of the cinematographic value, but because it meant so much to me when I was a child. And I was stunned to see that the things that I remembered fascinated me were quite different in the show. Some weren't even there. For example I remembered that the show was called Blake's 7 but that one of them died in the second episode, which I attributed to British humour. But no, that happened at the beginning of season two. The introduction and music I remembered to be dark, impressive and scary. They were really funny now. There were scenes I remembered in a completely different way, with more emotion and action and the things that happened in the show had another sense altogether.

The structure of the series is funny to follow. The crew of seven was always only of six people. The computer counted as the 7th man. In the second season Gant dies so they are temporarily left with only 6. In the third season Blake leaves the show as well as others of the crew, only to be replaced by other actors and another computer. So they are 5 people and 2 computers and no Blake in Blake's 7. The only constant things are the ship, which is destroyed at the end of the third season, Avon and Vila. Oh, and sexy Servalan, the evil female villain. With a ship that can go anywhere in the galaxy, they always stumble in the same people! The ending was hilarious also, but you have to click here to see what I mean, I would hate to spoil it for you:
Click here for ending spoiler

Here is a sample of the show:

And here is a video from an interview with the actors interpreting Blake and Servalan, old now and talking fondly of the series:

But there are also good news, Blake's 7 could be revived! I found an April 2008 link that says Sky One has commissioned two hour-long pilots for a new Blake's 7 series! Here is also a BBC News entry.

Update: Blake's 7 will be back! I doubt it will pack the same punch, unless done right. BBC should have remade it, but it seems that it will be a SyFy show, which may not be a good idea. The news confirming the comeback can be found here: Blake's 7: Classic BBC sci-fi to return on Syfy channel

Tuesday, May 27, 2008

Default values for the ASP.Net Login control

Well, I don't know who still uses it, but I had this old application that we reused and it had a Login control in the login page (obviously!). And I got tired of always entering the name and password every time I tested the site, so I went to the username and password TextBoxes and added a Text="something" bit. And it didn't work.

Why? First of all the TextBox control with TextMode="Password" ignores the Text property completely. Then the Login control only acknowledges the values of the two TextBoxes on change.

The solution: add a value="something" instead of Text="something" and it will be added like an oldfashioned HTML attribute. And it will also fire Changed since the value the .Net control is string.Empty.

That's all, folks, remember to remove this in the production site :)

Friday, May 23, 2008

Microsoft Software Development Training 21-22 May 2008

Petru Jucovschi was nice enough to invite me to a software development training organised by Microsoft and partly presented by him. The first of the two days of training was about the theory behind software development practices like agile development and CMMI, part presented by Victor Mihail from Trilex. The second was about the Team System Foundation Server and Visual Studio 2008 Team Edition integrating for a better software development practice.

Well, I am working on remembering everything that was said and done and I will make some longer posts that will describe at length what was presented.
As a short summary, I was familiar with a lot of the concepts from the first part, since I've done a little research into agile development in order to pass it on to my managers. The theory and principles behind Agile were detailed, then the major Agile methodologies were described, including eXtreme Programming, Scrum and (very shortly) MSF. Also concepts of Test Driven Development.
The second part was also partly known to me as I have seen another similar presentation at the previous RONUA meeting, but this time it lasted for 8 hours, so I had the opportunity to ask questions and see things done step by step. Basically, TFS is the tool that combines project management, business analysis, architecture, development, debugging and testing into a single useful bunch. The issue tracking is linked to reporting, integrated with Microsoft Project and Microsoft Excel and concepts like Continuous Integration are directly available along a better source control solution than Visual Source Safe. Bottom line, I can barely wait to start work in a rational, managed way.

Stay tuned for the details.

Wednesday, May 14, 2008

Dynamic GridView with paging throws 'Object reference not set to an instance of an object.'

Well, someone asked me today about why his dynamically generated GridView throws a 'Object reference not set to an instance of an object.' exception at DataBind when he sets AllowPaging to true. I replicated the error and looked at the stack trace, it looked like this:
   at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)
at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean addFirstLastPageButtons)
at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
at Malaka.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\ScrollableFixedHeaderGrid\Malaka.aspx.cs:line 14
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

So, the error occurs at the BuildCallbackArgument method in the GridView object. I used Lutz Roeder's Reflector to see the source of the GridView object and I got to this piece of code:
        private string BuildCallbackArgument(int pageIndex)
if (string.IsNullOrEmpty(this._sortExpressionSerialized))
this._sortExpressionSerialized = this.StateFormatter.Serialize(this.SortExpression);
return string.Concat(new object[] { "\"", pageIndex, "|", (int) this.SortDirection, "|", this._sortExpressionSerialized, "|\"" });

The error appears to be thrown by the StateFormatter property returning null. It's source is:
        private IStateFormatter StateFormatter
if (this._stateFormatter == null)
this._stateFormatter = this.Page.CreateStateFormatter();
return this._stateFormatter;

Got it? It assumes you already added the GridView to a Page. Well, this is the usual issue with the GridView.

Solution: Add the GridView to your Page (or to a fictive Page) and then DataBind it.

Sunday, May 11, 2008

Body Language

There are a lot of movies about aliens, creatures from another world, coming here for whatever reason and trying to communicate. They usually are well versed in English, but sometimes they use some other language like the Klingons or they snarl and rip you to pieces or, sometimes, have to find a protocol of communication first, by using mathematics, gestures or music (like in Close Encounters of the Third Kind). Yet none of those creatures astound me more than some of the people on Earth doing things I could never ever imagine doing and using it to communicate.

One of these people is Michael Moschen, who uses juggling as language of sorts in order to comunicate his perception of time, space and movement. You can watch a small demo he gave at TED and have your own say:

You can see that he uses separate elements to create, to build, larger ansembles. He uses juggling moves like a language, with structure and building blocks and architecture. Now, what would we do if aliens would want to talk to us by moving objects around and making sounds with their limbs? Would be build a computer that can be programmed by juggling? Would we immediately fly in Michael Moschen as the Earth ambassador? Or is it too science fiction to imagine alien creatures communicating in the same way some people do?

Saturday, May 10, 2008

Bucharest RONUA meeting. 9th of May 2008

The level of satisfaction one gets from anything is inversely proportional with the level of expectation they have. I knew that in the RONUA meeting on the 9th there will be only two presentations, with subjects I can't use right now and with speakers I didn't exactly feel compelled to listen to. So, of course, I liked it!

But there was something more. Both speakers seemed more mature, more anchored on the subject than usual. It's not that Aurelian Popa did not seem overly arrogant or that the ever helpful Petru Jucovschi didn't have a lot more information than was humanly possible to present in the alloted time and resources, but their performance seemed way better than I was used to.

The subjects were pretty clear: WPF and VS TFS. If you are scared by acronyms you probably shouldn't be reading this :), but I did provide tooltips for them, so move the mouse over them.

The first presentation showed an ObservableCollection (new stuff from .Net 3.0 which allows notification on changing elements) containing SolarSystemItems, objects that encapsulated an image, a diameter, a name, an orbit and a temperature. It started from a simple listbox then, by changing only the presentation (Style) and the way data is presented (DataTemplate) in the XAML file, it gradually changed to a picture+details listbox, a combobox also with picture and everything, then with a details box next to it showing the current selected object, with filtering, sorting, grouping, etc. The grand finale was making the same listbox look like a solar system image, with selectable planets and the details on tooltip, with orbits and everything. The demo can be downloaded and seen at Beatriz Costa's blog.

The second presentation I was more interested in, since our company plans to use Visual Studio TFS to manage our work. Since that would mean a shift from the current Excel+Email+Messenger, I was glad to learn that the TFS learning curve is steep and usage can also start from source control only, moving ever so slightly towards a comprehensive development strategy, with testing, reporting, configurable XML files that describe any process (with out of the box or downloadable versions for MSF, Scrum, XP, CMMI) , etc.

I was surprised to see that not only the session was free, but we also received some gifts from Microsoft for attending. That included the MSDN subscription that I won, the WPF book that my colleague won, a kit with all kinds of goodies that I can't remember won by our boss and 40% discount for some Microsoft products for each. We cleaned house! No free food though :)

My conclusion was that the RONUA meetings are (as far as I have seen) the most techie, interesting and to the point Microsoft events. The MSDN briefings are nice, too, with more budget allocated to them and so on and so on, but they will never be able to replicate the sort of "gang of IT people" feel of the RONUA's meetings. And with people that are ever more implicated and interested in presenting, it can only get better.

Friday, May 09, 2008

Great tits cope well with warming

Reading the BBC News today I've stumbled upon a fascinating article called "Great tits cope well with warming". Let me quote a little so you can understand what it is about: The research uses a long record of great tits in a breeding site at Wytham Woods near Oxford, where observations began in 1947. "We think it’s the longest running population study of wild animals anywhere in the world where animals are marked (ringed),"

So, warming doesn't negatively affect great tits! You can warm them yourselves or get someone else to do it. Isn't that wonderful? =))

Update 9 Sep 2009: Great tits found hunting bats for food

Wednesday, May 07, 2008

Getting rid of the annoying flash/shockwave commercial in MSN messenger

It started with web sites. They would put a small picture in the header of the site that on mouse over would fill the entire page with rubbish. We fixed it, usually, by using some utilities or by changing the ip of the offending ad to some non-sense ip.

Now they used the same ploy for Windows Live Messenger (formerly known as MSN). One could say that Microsoft doesn't need more money, but apparently they do. On YOUR expense! So, how can one get rid of the annoying flash ad at the bottom of MSN? Easy! Follow these steps (tested on version 8.5):
  1. Open folder Program Files\Windows Live\Messenger
  2. Copy file msgsres.dll to msgsres.dll.bak
  3. Find a text or hex editor that does not break a binary file on save (That means NO Notepad or Wordpad). Myself I used the text editor from FAR Manager, but there are tons of good freeware editors online. Just Google it.
  4. Open the msgsres.dll file with the text editor, look for
    <Element Id=Atom(AdBrowserCont) Layout=VerticalFlowLayout(0,0,3,0) Visible=false Padding=Rect(8,2,0,2)>
    and replace one letter from AdBrowserCont. I changed the A to an X, but it doesn't matter. What is important is not to change the length of the file; just replace, do not insert or delete anything.

That is all. The ad container will still appear and show the Windows Live Messenger window, but no ad will be loaded.

Of course, this is the do-it-yourself version. There is an utility that is largely recommended on the web to fix MSN, Windows Live Messenger and Yahoo Messenger ad issues, and that is A-patch, but I haven't tested it yet.

Tuesday, May 06, 2008

How to patch ASP.Net Ajax in order to work on servers that add stuff to your page automatically

Thanks for Raheel for presenting me this problem and the opportunity to fix it.

As you may know from a previous post of mine, Ajax requests differ from normal requests. At the Page level, the rendered content is changed to reflect only the things that changed in the UpdatePanels and the format is different from the usual HTML and it is also very strict. Any attempt to blindly add things to the string sent from the server will result in an ugly alert error. And who does indiscriminately add ugly content to your Ajax requests? "Free" servers that inject commercials in your pages!

So, what can you do? Patch the Ajax engine to remove the offending ads. Here is a simple example for a server that added crap at THE END of the content, crap that did not contain any '|' characters. This is important, as the patch looks for the last '|' character and removes all the things after it.

C# Code

private void LoadApplyPatch()
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (sm == null) return;

string script =@"
if (window.Sys&&Sys.WebForms&&Sys.WebForms.PageRequestManager) {
Sys$Net$WebRequestExecutor$get_responseData=function() {
if (arguments.length !== 0) throw Error.parameterCount();
if (!this._responseAvailable) {
throw Error.invalidOperation(String.format(Sys.Res.cannotCallBeforeResponse, 'get_responseData'));
if (!this._xmlHttpRequest) {
throw Error.invalidOperation(String.format(Sys.Res.cannotCallOutsideHandler, 'get_responseData'));

var content=this._xmlHttpRequest.responseText;

// this is the added code, the rest is taken
// from the ASP.Net Ajax original code
var index=content.lastIndexOf('|');
if (index<content.length-1)

return content;

Page.GetType(), "adMurderer", script, true);