Friday, 30 May 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);
Page.Header.Controls.Add(hgc);

}
. 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, 29 May 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, 27 May 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, 23 May 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, 14 May 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
{
get
{
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, 11 May 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?