Friday, December 20, 2013

Automatic Numbering for the Primary Key Column

Many of you might have come across an issue of Auto incrementing while working with database products. Truly speaking, recently I also came across one silly problem and thought of sharing it here.
Imagine that at some point of time, you might want to send your new data to a back-end database. If your application supplies the auto-increment values, then
  • what will the database do?
  • what if application receives duplicate values from different client applications?
The answer is that these auto-increment values are never sent to the database because the auto-increment column in the database table will provide a value whenever a new row is added. After each new row is added, the back-end database table generates a new-increment number and then your application will query the database to get the newly created number. Your application will then update its primary key number to the values that came from the database. This means that all the foreign key references will need to be updated as well.

Another problem, what happens if you add new rows in your application to generate auto-increment values of 1 to 100 and then send these rows back to the database table, and the table already has 10 rows???

When the first row is sent from your application, it has an auto-increment value of 1. The new auto-increment number created in the database will be 11. Your application queries for the 11 and tries to change the 1 to 11. Guess, what will happen here? Of course, an EXCEPTION will be thrown because 11 is already in your table.

The solution to this problem is, set AutoIncrementSeed to -1 and set AutoIncrementStep to -1. This will cause negative numbers to be generated; they won't conflict with the values coming from the database because the database doesn't generate negative numbers.

Hope the above small tip will save your lot of time in debugging the code to find the root cause. Enjoy learning !!! 

Saturday, December 14, 2013

Formatting strings in binding

Recently I get a chance for a code walk through of an WPF application in order to achieve better performance. So, during my review process, I came across this below code snippet:

        <TextBlock Text="{Binding Path=FirstName,Mode=OneWay}"/>
        <TextBlock Text=" "/>
        <TextBlock Text="{Binding Path=LastName,Mode=OneWay}"/>

If you will closely analyze this given snippet, you will definitely get a way to optimize it. Well, I am talking  about formatting the string as well as binding part.

As most of you are aware that we have a property named StringFormat since .Net 3.5 SP1. So, why can't we use this StringFormat for our binding too. If you want to change the above code to incorporate StringFormat, then it will something look like:

                    <MultiBinding StringFormat="{}{0} {1}">
                        <Binding Path="FirstName" Mode="OneWay"/>
                        <Binding Path="LastName" Mode="OneWay"/>

Now here one can clearly see the double benefit of writing such code. First benefit is, instead of using two bindings, one can be used and another benefit is instead of using three TextBlocks only one TextBlock can be used.

This may help in achieving performance especially when used as a DataTemplate of an ItemsControl with huge number of items.

Please note that {} part at the beginning is an escape sequence, otherwise XAML parser would consider { to be the beginning of markup extension.

Tuesday, December 10, 2013

Resizing TextBlock with resizing of window

When dealing with XAML, many of you might have came across resizing issues in which your control is getting resize but the text/image inside the control is not getting resize as per the window size(it can be other user control). So, to deal with this Microsoft provide us with a Viewbox control, in which user can set the resizing aspects as per the need and requirement. So, this article focuses mainly on the properties provided by Viewbox control with proper sample code and output. MORE...