As I was curious to see how big is the difference between these interfaces I did some performance testing.

This are the results:

I did the perfomance testing under Windows Vista 64bit edition with the tool HDD Speed Test Tool by Marko Oette.Regarding the eSATA interface I would like to say that the hot plug works (connect/disconnect while windows is running) and that while working with my computer I encountered sometimes some strange problems. The whole computer was blocked especially when I used applications that show an open file dialog or something similar. As soon as I unplugged the external HDD everything worked well. I would say that eSATA does not work 100%. This may be especially frustrating when making a backup of very large files and during the backup the HDD disappears (which happened to me). I don't exactly know if it is a driver, hardware or Windows Vista problem.

Unfortunately the DataGrid of the WPF-Toolkit does not provide a text highlighting feature out of the box; but sometimes it is very usefull for the users to be able to search for text or numbers in the datagrid to find the interesting information more quickly.Tomer Shamam did a very good work which highlights a search key in different colors depending on which column they were found.

The only things which I don't like on his solution are:

It works only if the datasource of the datagrid are objects

That you have to define a style for every property you want to be highlighted in the datagrid which may be a big work if you have 15 properties for example

When a row with a match is selected you do not see anymore which cell was highlighted

For my pupose I don't need the feature of the different coloring because I want only to highlight all cells in the datagrid where the search term is present in the same color. To resolve all of the above issues I changed the code of Shamam slightly.

As far as I understood this code defines a new dependancy property on the datagrid where the search term is stored and another dependancy property on each cell which stores wheter it is a match or not. Through the style which is defined in the Resource section of the page the "IsMatch" property is used to format the cell accordingly.

This is my modified XAML code to search the text of the datagrid instead of the property. Here you can also change the different colors if you like.

Tuesday, November 17, 2009

Sometimes it happenes that we have to format the data which is stored in an SQL Server Database for an older system which requires the data to have a specific length. To achieve the length different datatypes are filled differently (at least in my case).

Lets take this example (we assume a length of 8):- numbers have to be filled with zeros on the left (e.g. 66 -> 000066)- strings have to be filled with spaces on the right (e.g. 'abc' -> 'abc ')

How can we achieve this?

Well the first problem is that we have to measure the length of the data and SQL Server provides two functions for that: 'LEN' and 'DATALENGTH'

This is not a problem you would say but it is because they behave differently. Look at the following examples:

Well this "strange" behaviour is because of what these functions do. Let's see the definition from MSDN on this:

LEN (Transact-SQL)Returns the number of characters of the specified string expression, trailing blanks.

DATALENGTH (Transact-SQL)Returns the number of bytes used to represent any expression.

So in principle I would say: if you want to know the number of characters of a value then you should use DATALENGTH for string values ( nchar, text, nvarchar,...) and LEN for numbers (int, float, decimal,...).

The next problem when filling values is the NULL value. According to the definition of the two functions is LEN(NULL) equal to NULL and DATALENGTH(NULL) also NULL. This may be a problem since I have to provide blanks or zeros if the value is NULL (e.g. ' ', 00000000).

The third problem is on how to fill the value with the requested fillers (zeros and blanks)? Well for this SQL Server provides a very nice function: REPLICATE

This function takes as input a character which should be replicated and a length. E.g.:

Of course written like this it does not make much sense since we know in advance which part of the case statement is executed; but imagine you write in the place of the "null" or "99" the name of a column, then it makes perfectly sense. You can now easily write a case statement for string values.

Tuesday, November 3, 2009

Recently I created a photoshow from my photos of the summer and there I wanted also to add a Google Earth flight, so that everybody has an idea where this location is; and also because its simply cool :-)

On the internet they say that the pro edition has such a functionality but it costs 400$.I tried then severeal screen capturing tools CamStudio or AutoScreenRecorder. They may be good to capture the screen to show other people how to perform some actions but for the Google Earth flight they are too slow. If they are capturing the flight isn't smoothly anymore.Fortunately I found a very good tool which is also used to capture games. It is called Fraps and does a very very good job. I tried it with the OpenGL as well as with the DirectX mode of Google Earth and it works well with both of them. I think that after the test period it makes a watermark on the video but I can live with that.

Tuesday, September 29, 2009

Recently I played a bit with the "new" Entity Framework (basically the Object Relational mapper of Microsoft). It seems to be made quite good and it makes it very easy to work with objects in your application without having to map them back and forth from and to SQL. Also the possibility to launch LINQ queries over the data is very interesting because this avoids many errors and improves usability.But I missed something: a fulltext search.SQL Server has a very good fulltext search integrated and it would be nice to be able to perform queries through LINQ with it; but since Microsoft wanted to support also other database management systems they had to use only standard SQL for their mappings.

Faced with this problem I resolved it by writing my own pseudo fulltext search by using the possibility of creating dynamic LINQ queries.First I will show You the code and then I explain a bit what I have done.

public static class ObjectContextExtensions{/* /// <summary> /// Searches in all string properties for the specifed search key. /// It is also able to search for several words. If the searchKey is for example 'John Travolta' then /// all records which contain either 'John' or 'Travolta' in some string property /// are returned. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="searchKey"></param> /// <returns></returns>*/ public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey) { return FullTextSearch<T>(queryable, searchKey, false); }

/* /// <summary> /// Searches in all string properties for the specifed search key. /// It is also able to search for several words. If the searchKey is for example 'John Travolta' then /// with exactMatch set to false all records which contain either 'John' or 'Travolta' in some string property /// are returned. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <param name="searchKey"></param> /// <param name="exactMatch">Specifies if only the whole word or every single word should be searched.</param> /// <returns></returns>*/ public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey, bool exactMatch) { ParameterExpression parameter = Expression.Parameter(typeof(T), "c");

I created this method as an extension method for an IQueryable<T>.This means if you have your ObjectContext (from the Entity Framework) then when accessing all objects of some type e.g. "context.Customers" then this returns an IQueryable<Customers>This means that You can call the above method like:

context.Customers.FullTextSearch("serachkey");

or

context.Customers.FullTextSearch("searchkey", true);

which searches only for whole words also if they contain spaces. This gives You then again an IQueryable<Customers> which contains only the filtered objects.

Ok. Now something about the code.This method "simply" creates a lambda expression which gets all the string properties of the type T, calls the "Contains" method with the search key as parameter on them and connects them with an OR. This means that it would be similar to do something like:

Thursday, August 13, 2009

In response to the question of Jonathan of my post on expression trees I will post here some code on how an expression tree can be analyzed and used to create your own object relational mapper.

The core of the app is the expression tree visitor. Practically it traverses all the expressions of a LINQ expression and creates the appropriate SQL instructions for it. To get the correct column and table names it uses an interface called IMappingProvider which makes it possible to have different mapping providers. I created an AttributeMappingProvider which provides the mapping by defining attributes on the classes and properties, an XmlMappingProvider which reads the mapping information from an XML file and a DBMappingProvider which reads from a table in a database. My ORMapper is able to create Select and Delete queries by specifying a LINQ expression as where clause.

But enough theory; here is some code :-)This is the core method which dispatches the incoming expression and calls the appropriate method:

You can see here that for binary expressions (e.g. =, &&, ||) I always open a parenthesis; this creates also unnecessary parenthesis, but makes sure that boolean conditions are evaluated correctly. Then the left expression is evaluated, the operator is added, the right expression is evaluated and finally a closing parenthesis is added.This is the dispatcher method for the operators:

To get the correct string to add to the sql query I wrote a little method which performs also escaping of the single quote. Additionally it calls the to string method with the invariant culture if the object supports it.

Thursday, June 25, 2009

Today I had the problem that I had to set up input validation on an edit form. Among other controls there was also a NumericUpDown (NUD) control and I thought: well if I specifiy the min value and the max value then the user is not able to input a wrong value;

This code prints the current value of the NUD to a message box:

MessageBox.Show(this.numericUpDown1.Value.ToString());

You can see an example for this in the following screenshot:

This works fine, but there is one exception. It is also possible to leave the NUD blank. If you show then the value of the NUD again with the above code then you get the last value that was present in the NUD control; as shown below:

This can also be explained. The value property of the NUD control is of type decimal in order to deal with the different types of numbers. The problem is that it is of type decimal and not decimal? (this is the nullable version of decimal) and therefore it has to have value. Now is the question which value to set if the control is empty: 0, -1, decimal.MinValue or decimal.MaxValue?? None of these value really expresses what the user has entered; namely nothing.In a forum in the internet I read that a workaround for this issue is to check the Text property of the NUD control; but as you can see below the NUD control does not have such a property!!

Fortunately there exists a solution to this problem. If you want to check if the user has entered a number into the NUD control you can use this code to check this:

if (string.IsNullOrEmpty(((Control)this.numericUpDown1).Text)){ /* the user entered nothing */}else{ /* the NUD contains a valid number */}

Wednesday, June 24, 2009

In a very small personal project I needed to parse an HTML page. I thought that HTML = XML and so I tried to load the response string into an XDocument; but I was too optimistic. I got a lot of errors from the XDocument.Parse() method. I read on the internet that it is only possible to do this if the web page is XHTML and follows all the standards. But there are a lot of web pages out there which never heard nothing about standards. A good example are pages from Microsoft. This page of the Sysinternals Suite for example produces 53 errors and 14 warnings when validating it with the W3C validator!!

There is a solution to the HTML parsing problem for C#. There exists a project called HTML Agility Pack on CodePlexx which provides a mechanism to parse an HTML string and to navigate it like ian XmlDocument. It works great but it does not provide support for LINQ queries which is very nasty if you are accustomed to it (like me :-))But also for this exists a solution. Since the source code of the HTML Agility Pack is open for everyone Vijay Santhanam created the ToXDocument() method, which converts the HtmlDocument to XDocument. This is then queryable with LINQ to XML. The post about this and a link to his project can be found here.

It would be nice if they would add direct LINQ support to their HtmlDocument class but maybe they will do so in the future...

Thursday, June 18, 2009

While trying to speed up an application I searched the internet for information about reflection. I use reflection quite often when I have to write a special XML serializer or when I have to write a simple OR mapper. The problem is that it is slow. I understand now also because you have to specify the type when you use the XmlSerializer of the .Net framework. Internally it generates IL code which speeds up the serialization a lot because the intermediate code is much faster than direct reflection.

Regarding this problem I found a very interesting blog post which you can check out here.

There a comparison is made between the direct reflection, reflection by using Reflection.Emit and by caching Reflection.Emit operations. As you can see there from the test results the cached reflection is about 8 times faster!! This may not be of importance if you serialize only 100 objects but if you serialize thousands or millions of objects than this makes a big difference.

I tried his code in a project of mine where I use quite a lot reflection and indeed the operation finished about 6 times faster!

If you do a lot of reflection in your projects you should definitely give a look at this code. It's worth...

Monday, June 15, 2009

Today I had to solve a common problem. The customer sad that the application is too slow; substantially the data import from an XML file into the object model is too slow. As every developer knows, performance problems are very nasty and it means also that you have to break your nice object oriented structure to gain some performance boosts :-(So I searched in the imort routine for code parts which can be optimized. I was able to change some code but that brought only a very small performance gain.After some time I decided to switch off the logging that states how many percent of the elements were already imported. That message was written to the console window and since the operation required about 15 seconds it was interesting to see at which point the import is.Then the surprise. After switching off the logging the data was imported in about 1,3 seconds. I knew that writing to the console is slow but that it is so slow...To solve this problem now I tried first to use an asynchronous event logging. It worked also quite well, but when the data was already loaded and displayed the logger wrote for some seconds longer to the console by indicating that 97% of the data was loaded now.This was also not a choice. Then I decided that it would be a good idea to switch off logging when running in release mode and turn it on when running in debug mode. So I have all informations on hand whenn looking for errors and the performance for the customer.To realize this I implemented in my logger a the method "LogDebugEvent" and "decorated" it with the "Conditional" attribute:

This means that the compiler decides if the method call is executed or not. If you compile in Release mode than all calls to this method are removed from the code as if they do not exist. If you run in Debug mode then all calls are executed. This can be very usefull especially if you have code which slows down your program and is only for debug purposes.Important is that it matches exactly the constant that you want to use. It is casesensitive!! In the case of debug it is as you can see in the example above "DEBUG".

More informations about the ConditionalAttribute can be found here and here.

Tuesday, April 21, 2009

Lately I had to implement my own LINQ to SQL "converter" which uses a custom mapping stored in the database. It is even not as difficult as I thought. You have "only" to recursively parse the expression tree and convert it to the correcsponding SQL instructions.The only thing I had some problems with were local variables or parameters.Consider the following code:

when analyzing the expression tree then You see that the right expression of the BinaryExpression is of type ConstantExpression. Normally ConstantExpression contain directly a value. But this is not the case here! The console writeline produces the following: "ConsoleApplication1.Program+<>c__DisplayClass0"The value of the constant expression is an anonymous object which is created on the fly. This object has a field which is named a and that field contains the value.Therefore I had to do the following:

Instead of evaluating the expression which is associated to the MemberExpression.Expression I used the Member and casted it to a FieldInfo. Then I called the GetValue method passing it the value of the ConstantExpression which is basically the object. In this way I finally got the correct value of a.The same works also for method parameters. Consider this code:

public static void FromMethod(int a){ Get<Person>(p => p.Age == a);}

there we have exactly the same problem and it can be resolved in the way described above.

Wednesday, March 25, 2009

Recently I faced the problem of inserting a big amount of data into the database. The point is that multiple tables are affected which are connected with foreign keys. After some research I found OpenXML as one possibility. You create a stored procedure where you read your XML string into an "XmlDocument" which you can then query by using XPath and inserting, updating or deleting the tables occordingly.

The other possibility I found is SQLXML which is now available in version 4.1. It is delivered with SQL Server but not with the Express version. If you need it you can download it from here. SQLXML allows you to bulk insert data from an XML file by using a so called annotated XSD schema where the mapping is defined. It is not very difficult to define the schema, only recursion and special cases are a little bit difficult to handle. I was not able to make recursion work with identity values coming from the database. Anyway; I did some performance tests where I had a structure like:Table ATable B has a foreign key to Table ATable C has a foreign key to Table Bwhich corresponds to the following XML structure:<root> <a> <b> <c></c> </b> </a></root>

I created an XML file that contained 100 A's; every A contained 100 B's and every B contained 100 C's. This gives a total of 100 A's, 10.000 B's and 1.000.000 C's. To insert all them and updating the foreign keys it took 84 seconds. This is quite fast for such an amount of data. I think that this is the fastest way to bulk insert data into SQL Server (if I'm wrong You are welcome to coment on this).If you want to use it with .Net add a reference to "Microsoft SQL Server Bulk Upload" or something like that.I don't remember the exact name but as soon as I find the link of the code sample I will post it here.

The problem is that SQLXML cannot be used for updates. There exist so called updategrams or diffgrams but I didn't try them.

My girlfriend asked me look for a small program wher she can add notes and delete them if necessary. I serached the internet and there are a lot of programs out there to do this, but they are all very complex and offer a huge amount of feautres that she does not need. Since I'm a programmer I decided to write one :-)

As the title says it is a really simple program. Here a screenshot of it:

and this is how it looks with some notes:

Obviously by clicking the trash the note is deleted. All the notes are saved in a simple XML file in the isolated storage of the user. This means that you can run the application from a readonly storage (like a CD) and it saves all Your notes in your user profiles folder.

If someone is interested in this very simple tool You can get it from here.

Thursday, January 29, 2009

Today I encountered a nasty problem. I clicked on an e-mail address on a web page and instead of opening Thunderbird with a new mail message I got the following error "Der Vorgang kann nicht ausgeführt werden weil der Standard-Mailclient nicht richtig installiert ist.". This means something like "The operation can not be performed because the standard mail client was not correctly installed.".I told Thunderbird to ckeck if it is the standard mail client under Extras -> Settings -> General -> Standard mail client (I don't know if that is correct because I have a german version); it told me that it is the standard mail client.

Finally to make it work I had to go in the Internet Explorer (7) to Extras -> Internet options -> Programs and there I selected Thunderbird as standard mail client.

Strange thing; but sometimes things stop to work without any obvious reasons :-)

Wednesday, January 21, 2009

Today I passed the microsoft exam 70536 Microsoft .NET Framework - Application Development Foundation. I found it very interesting to prepare myself for this exam because despite that I program every day at work there were a lot of things that I simply didn't know that they exist. Other things I never understood completely and this exam helped me to find out how and why it works in this way. The exam itself was not very easy because the questions are very detailed and sometimes you simply have to have used some classes in order to know if a parameter is passed through the constructor or set with a property. The most difficult are the questions wher you have a list of steps and you have to bring them in the correct order; but not all steps may be needed. I find these kind of questions especially difficult because sometimes there is not only one way to accomplish the specified target. Anyway I am happy that I passed the exam and that now I can prepare myself for the next exam... a never ending story :-)

[Update]Now I got access to the MCP website from where I also got the logo which I proudly put on the right menu bar :-)

Wednesday, January 7, 2009

Today I faced the problem to update the App.config. There exist several solutions out there but I was not able to find a simple one. Therefore I decided to try out the new Linq to XML feature of .NET 3.5 and wrote the following method which updates the value of a specific key in the App.config file: