Petes techie blog

A blog written for two people. Me and You.

.Net Site Speed and Optimisations

| Comments

I get obsessed with website load speeds, I can spend hours looking at PageSpeed and ySlow and trying to work out how to increase the speed of the website for the client. Now that I can get a speed result using Webmaster Tools from Google (loook under labs) I now have a way of measuring my website performance over a period of time.

Currently I am lookling at combining javascripts and CSS. In .net 3.5 the script manager can combine scripts for me both resourced scripts and local files. BUT I dont like using scriptresource.axd for some reason, I guess it is because the scripts are static but Im getting asp.net to generate it for me. Instead they should be static local files and let IIS just cache the files like normal static files.

Scriptmanager lets me set a path where the combined script can be stored: More info here: [http://www.hanselman.com/blog/ASPNETAjaxScriptCombiningAndMovingS criptResourceaxdsToStaticScripts.aspx](http://www.hanselman.com/blog/ASPNETAja xScriptCombiningAndMovingScriptResourceaxdsToStaticScripts.aspx)

This is only of benefit if you are using plenty of .net 3.5 features (calendar extenders etc) though.

Null-coalescing Operator in C#

| Comments

I always like elegant looking code. I find it very ugly having if statements to compare to null. In c# you can use the ?? operator which basically says if left hand side is null then use right hand side else use left hand side.

Example:

string r = null;

Console.Write(r ?? “”);

sweet.

SelectedValue Property Cannot Be Set Declaratively

| Comments

I am binding a detailsview to a datasource and wish to use a drop down list (ddl) for one of the fields. It is fine to bind the ddl to another datasource but I couldnt get the selectedvalue to be set due to errors like: ‘SelectedValue’ property cannot be set declaratively.

In the ide you can’t set SelectedValue using intellisense or the properties window (which makes you think it isnt a valid property). BUT you can set it using the databindings window of the ddl (click the arrow to the right of the control and choose “edit databindings”.) BUT you will still get the above error if you try to use Bind or eval.

The solution: In the source use the following syntax:

SelectedValue=’<%# Bind(“TypeID”) %>’

USE SINGLE QUOTES!!!! It doesnt work if you use double quotes. I dont know why this works I imagine it is because of a pre-validator which ignores single quotes. I’m using VS 2008.

SQL Date to Midnight

| Comments

This post is the reason why I set up this blog. I always look this function up so it makes sense to be in one place!

Two versions, the first one is my favourite:

print dateadd(dd,0, datediff(dd,0,getdate()))

print convert(nvarchar(12), getdate(), 101)

Iif in C#

| Comments

bool ? resultIfTrue :resultIfFalse

Update (15 June 2012): And its called a ‘Conditional operator’ if you ever need to search for it!

Difference Between Dates in Whole Months

| Comments

1
2
3
4
5
6
7
8
9
10
11
// No of years * 12 + no of months + (-1) if startday is greater than endDay
private static int monthDifference(DateTime startDate, DateTime endDate)
{

int monthsApart = 12 * (endDate.Year - startDate.Year) +

endDate.Month - startDate.Month + ((startDate.Day - endDate.Day > 0) ? -1 : 0);

return monthsApart;

}