Posts

Showing posts from June, 2010

Removing duplicate calls on an AspNet Page Load by using the MasterPage

This is often something that is never apparent but can improve performance. On the master page , you need to make a call, for example, to get the MembershipUser. MembershipUser user = Membership.GetUser(); Then on the page you need to do the same so you copy the code again.  This results in two calls. For a current project, this means two WCF calls so there is a significant latency added to page rendering.  A better solution is to make the User public on the master page and then use it (and avoid the second call).   On the master page, change it to a self-initializing property (and avoid putting initialization into events like Page_Load) MembershipUser _User public MembershipUser User { get { if (_User == null ) { _User = Membership.GetUser(); } return _User; } }    and then on the child pages use: protected v

Improving XML User Defined Function Performance

Doing some code review, I came across a piece of code which giving   Msg 8114, Level 16, State 5, Line 1 Error converting data type nvarchar to numeric.   The likely cause was someone entering a ‘n/a’ in the field (the curse of XML coming from slack systems). Create FUNCTION [dbo].[GetSellingPrice] ( @ Data xml ) RETURNS decimal (20,2) WITH SCHEMABINDING AS BEGIN declare @ Return decimal (20,2) select @ Return = CASE WHEN @ Data . value ( '(//Property/@SellingPrice)[1]' , 'nvarchar(20)' ) IS NULL THEN 0 ELSE @ Data . value ( '(//Property/@SellingPrice)[1]' , 'decimal(20,2)' ) END RETURN @ Return END Even with good data, this code is not ideal for several reasons: Multiple calls to the .value function to get the same data Using nvarchar when va

When writing .Net Providers do not forget to check for orphan name-value pairs

Today while I was reviewing some code I saw reasonable code (shown below) which I modified to be better code. Why do I say better? Well, a common production bug is mistyped or mis-cased attributes for providers in the configuration file.  These bugs can take a long time to track down – it depends on someone with sharp eyes!  The code modification will immediately identify any orphaned name-value pairs, a typo breaks the initialization and the fix happens in seconds, not hours.   The key is to remove all of the name-pair values that you are using (as they are consumed) and then check if there are any orphans after the base processing.  All of the good provider sample codes that I have seen does this. Once I saw the logic, it has becomes a regular code-review item. The pattern is a good one for making deployments more robust – a typo is often hard to track down.   Reasonable Code   public override void Initialize( string name, System.Collections.Specialized.NameValueColle

Building Indexes on Views based on XML

When dealing with XML columns, you can improve search times by building indexes on the values extracted from the XML.  The index persists the value for searches so there is no need to re-extract the value until you actually retrieve the record after finding it.   Tonight, I was helping a friend and it required a view from an inner join of two different tables and getting values from each table’s XML to be coalesce. So the logical solution is to create a VIEW and then index the xml values. I could create the columns for each value and proceed that way – but it consumes  resources that are not absolutely required.   Wait! You cannot create indexes on views containing XML data type methods. If you try, you will get this error message: Msg 1985, Level 16, State 1, Line 1 Cannot create index on view 'VerraTerraDatabase.dbo.CurrentData2'. It contains one or more XML data type methods.   i.e. If you try something like: Create view CurrentData WITH SCHE

Formatting Exports from Telerik RadGrid (PDF,Word, Excel)

I have been spending a bit of time trying to get a simple and elegant way to (re-)format an export from a RadGrid. The Css Styles (especially if you do not use the built-in skins) are not carried through to the export.   The export creates XHTML which is then passed to the appropriate engine to produce the download file. The solution (once you find it) is pretty clean.   First, I created a Css.Resx file which contains the following items: GridItem GridItemCell GridHeaderItem GridHeaderItemCell GridItem GridItemCell Each of these items contain a definition in classic style sheet format. color : #0000FF; font-size : 1.2em; So you can now define the appearance of each of these six regions (you can do more regions using the same pattern).   Setting up to detect the Export Telerik examples typically shows the use of an independent button which then sets a variable. We use a similar pattern but make use of the built-in commands