Saturday, August 18, 2012

Setting Culture in ASP.NET

Difference between Culture and UICulture


Culture defines culture dependent objects like currency, date, currency and number formatting.  This can only defined using a specific culture (ex: en-GB not en)

UICulture defines which resources (local and global) will be loaded for this page.  Both neutral and specific cultures.

Setting Culture dynamically at runtime


Culture and UICulture need to be set in the InitializeCulture event


protected override void InitializeCulture()
{
if (Request.Form["DropDownList1"] != null)
{
//can be neutral i.e. en or en-GB
UICulture = Request.Form["DropDownList1"];
//requires a specific culture ex: de-DE
Culture = Request.Form["DropDownList1"];
}
base.InitializeCulture();
}

Wednesday, August 1, 2012

Return nth index of a delimited string in TSQL

CREATE FUNCTION [dbo].[fn_SplitSelect] (@sep VARCHAR(32), @s VARCHAR(MAX), @index INT)

RETURNS NVARCHAR(MAX)
AS
    BEGIN
        DECLARE @xml XML
        SET @XML = N'' + REPLACE(@s, @sep, '') + ''
        
        DECLARE @ret NVARCHAR(MAX)

        SELECT @ret = Item
        FROM (
                    SELECT r.value('.','VARCHAR(5)') as Item, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS rownum
                    FROM @xml.nodes('//root/r') AS RECORDS(r)
                    ) t
             WHERE t.rownum = @index
        

        RETURN @ret
    END

Friday, July 27, 2012

T-SQL: Script to enter list of colours

I couldn't find a list, so i created one from the list of colours used by HTML. Enjoy :) Does anybody know of a website where you can find these kind of scripts?
/****** Object:  Table [dbo].[Colours]    Script Date: 07/27/2012 10:07:49 ******/
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'AliceBlue ', N'#F0F8FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'AntiqueWhite ', N'#FAEBD7')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Aqua ', N'#00FFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Aquamarine ', N'#7FFFD4')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Azure ', N'#F0FFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Beige ', N'#F5F5DC')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Bisque ', N'#FFE4C4')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Black ', N'#000000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'BlanchedAlmond ', N'#FFEBCD')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Blue ', N'#0000FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'BlueViolet ', N'#8A2BE2')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Brown ', N'#A52A2A')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'BurlyWood ', N'#DEB887')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'CadetBlue ', N'#5F9EA0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Chartreuse ', N'#7FFF00')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Chocolate ', N'#D2691E')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Coral ', N'#FF7F50')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'CornflowerBlue ', N'#6495ED')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Cornsilk ', N'#FFF8DC')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Crimson ', N'#DC143C')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Cyan ', N'#00FFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkBlue ', N'#00008B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkCyan ', N'#008B8B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkGoldenRod ', N'#B8860B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkGray ', N'#A9A9A9')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkGrey ', N'#A9A9A9')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkGreen ', N'#006400')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkKhaki ', N'#BDB76B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkMagenta ', N'#8B008B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkOliveGreen ', N'#556B2F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Darkorange ', N'#FF8C00')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkOrchid ', N'#9932CC')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkRed ', N'#8B0000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkSalmon ', N'#E9967A')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkSeaGreen ', N'#8FBC8F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkSlateBlue ', N'#483D8B')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkSlateGray ', N'#2F4F4F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkSlateGrey ', N'#2F4F4F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkTurquoise ', N'#00CED1')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DarkViolet ', N'#9400D3')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DeepPink ', N'#FF1493')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DeepSkyBlue ', N'#00BFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DimGray ', N'#696969')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DimGrey ', N'#696969')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'DodgerBlue ', N'#1E90FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'FireBrick ', N'#B22222')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'FloralWhite ', N'#FFFAF0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'ForestGreen ', N'#228B22')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Fuchsia ', N'#FF00FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Gainsboro ', N'#DCDCDC')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'GhostWhite ', N'#F8F8FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Gold ', N'#FFD700')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'GoldenRod ', N'#DAA520')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Gray ', N'#808080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Grey ', N'#808080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Green ', N'#008000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'GreenYellow ', N'#ADFF2F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'HoneyDew ', N'#F0FFF0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'HotPink ', N'#FF69B4')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'IndianRed  ', N'#CD5C5C')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Indigo  ', N'#4B0082')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Ivory ', N'#FFFFF0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Khaki ', N'#F0E68C')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Lavender ', N'#E6E6FA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LavenderBlush ', N'#FFF0F5')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LawnGreen ', N'#7CFC00')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LemonChiffon ', N'#FFFACD')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightBlue ', N'#ADD8E6')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightCoral ', N'#F08080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightCyan ', N'#E0FFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightGoldenRodYellow ', N'#FAFAD2')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightGray ', N'#D3D3D3')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightGrey ', N'#D3D3D3')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightGreen ', N'#90EE90')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightPink ', N'#FFB6C1')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSalmon ', N'#FFA07A')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSeaGreen ', N'#20B2AA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSkyBlue ', N'#87CEFA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSlateGray ', N'#778899')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSlateGrey ', N'#778899')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightSteelBlue ', N'#B0C4DE')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LightYellow ', N'#FFFFE0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Lime ', N'#00FF00')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'LimeGreen ', N'#32CD32')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Linen ', N'#FAF0E6')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Magenta ', N'#FF00FF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Maroon ', N'#800000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumAquaMarine ', N'#66CDAA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumBlue ', N'#0000CD')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumOrchid ', N'#BA55D3')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumPurple ', N'#9370D8')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumSeaGreen ', N'#3CB371')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumSlateBlue ', N'#7B68EE')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumSpringGreen ', N'#00FA9A')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumTurquoise ', N'#48D1CC')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MediumVioletRed ', N'#C71585')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MidnightBlue ', N'#191970')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MintCream ', N'#F5FFFA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'MistyRose ', N'#FFE4E1')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Moccasin ', N'#FFE4B5')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'NavajoWhite ', N'#FFDEAD')
GO
print 'Processed 100 total records'
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Navy ', N'#000080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'OldLace ', N'#FDF5E6')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Olive ', N'#808000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'OliveDrab ', N'#6B8E23')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Orange ', N'#FFA500')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'OrangeRed ', N'#FF4500')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Orchid ', N'#DA70D6')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PaleGoldenRod ', N'#EEE8AA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PaleGreen ', N'#98FB98')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PaleTurquoise ', N'#AFEEEE')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PaleVioletRed ', N'#D87093')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PapayaWhip ', N'#FFEFD5')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PeachPuff ', N'#FFDAB9')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Peru ', N'#CD853F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Pink ', N'#FFC0CB')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Plum ', N'#DDA0DD')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'PowderBlue ', N'#B0E0E6')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Purple ', N'#800080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Red ', N'#FF0000')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'RosyBrown ', N'#BC8F8F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'RoyalBlue ', N'#4169E1')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SaddleBrown ', N'#8B4513')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Salmon ', N'#FA8072')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SandyBrown ', N'#F4A460')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SeaGreen ', N'#2E8B57')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SeaShell ', N'#FFF5EE')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Sienna ', N'#A0522D')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Silver ', N'#C0C0C0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SkyBlue ', N'#87CEEB')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SlateBlue ', N'#6A5ACD')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SlateGray ', N'#708090')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SlateGrey ', N'#708090')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Snow ', N'#FFFAFA')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SpringGreen ', N'#00FF7F')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'SteelBlue ', N'#4682B4')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Tan ', N'#D2B48C')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Teal ', N'#008080')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Thistle ', N'#D8BFD8')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Tomato ', N'#FF6347')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Turquoise ', N'#40E0D0')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Violet ', N'#EE82EE')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Wheat ', N'#F5DEB3')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'White ', N'#FFFFFF')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'WhiteSmoke ', N'#F5F5F5')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'Yellow ', N'#FFFF00')
INSERT [dbo].[Colours] ([Colour], [Code]) VALUES (N'YellowGreen ', N'#9ACD32')

Wednesday, July 25, 2012

Using Resource Files in ASP.NET

There are 2 types of resource files, local and global resource files.  Local resource files are used for a specific page, while global resources can be used throughout a site.

Local Resource files


Local resource files must be stored in the ASP.NET folder App_LocalResources.  These files have the extension .resx and must be named like the page.  If the page is named About.aspx, then the resource page will be About.aspx.resx.  This will be the default resource file for the page.  In order to create a resource file for another language, you will need to create another file with the language 2 letter ISO code.  If you need resources for the German language for example, the file will need to be named About.aspx.de.resx.  You can also use specific languages like About.aspx.en-GB.resx

Implicit Localization


To easiest way to create a resource file, is to use the tool provided in Visual Studio. From the Tools menu, select Generate Local Resource.  This will create a resource file for the form you are viewing.

The tool will generate resource keys for every string property for every control


Explicit Localization


In explicit localization, you can manually attach a resource to a control's property like so

 

Global Resource files


Global resource files are stored in the App_GlobalResources ASP.NET folder.  Use these resources for text which will be used more than once in your website.  I typically use it for common words such as 'Save', 'Edit', 'Delete', etc.. These files also have the .resx extension.  In order to use them from mark up, use the same syntax as the explicit localization mentioned above


Using and accessing resources programmatically


There are 2 ways to retrieve values at runtime.  If the global resources are available in your project you can use the following syntax which also provides intellisense
string myText = Resources.SharedLocalizedText.WelcomeText;
If the file is not available at design time, you can use the following method
string myText = Convert.ToString(GetGlobalResourceObject("MyGlobalResourceFile", "WelcomeText"));

The first parameter is the name of the global resource file (without the extension) and the second is the resource key which you want to retrieve from the file

Thursday, June 21, 2012

Using Cache in ASP.NET


if (Cache["Name"] != null)

Label1.Text = "Hello," + (string)Cache["Name"];

else

Label1.Text = "Hello, guest! ";


Setting Cache Expiry


Cache.Insert("Name", "Kevin", null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

Thursday, June 14, 2012

How to: Add CSS file in ASP.NET at runtime

Adding CSS file in ASP.NET programatically

HtmlGenericControl si = new HtmlGenericControl();
si.TagName = "link";
si.Attributes.Add("type", "text/css");
si.Attributes.Add("href", "css/module.css");
si.Attributes.Add("rel", "stylesheet");
this.Page.Header.Controls.Add(si);

Tuesday, June 12, 2012

Themes

Creating a theme


Themes should be defined in the special App_Themes folder.

  1. Right-click your website in Solution Explorer, click Add ASP.NET Folder, and then click Theme.
  2. Within the App_Themes folder, you define an individual folder for each theme in your application. Example: Professional or Simple

You can then create skin files or style sheets in each of these theme folders.

Applying the theme


This can be done either on the page level by using the attributes Theme or StyleSheetTheme in the @Page directive.  The difference between Theme and StyleSheetTheme is that StyleSheetTheme sets properties before the page's controls properties are set, while Theme properties are set after the page sets the controls properties.

This means that if in the theme folder, we have set the label colour to Red and in the page we set the label colour to Green, the label will be red if using Theme but green if using StyleSheetTheme.

Or as commonly used, apply it to the whole website through the web.config in the system.web section

<pages Theme=”themeName”>

or

<pages StyleSheetTheme=”themeName”>

Changing theme at runtime programattically


In order to change the theme by code, use the PreInit event to change either the 'Theme' of the page.  If you want that the user changes the theme upon a button click or a change in dropdown, just store the value in  a session variable, and then set the session variable's value to the page.theme in the preinit event

protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = "Simple";
}

To change StyleSheetTheme at runtime, you can't do the same as the above or you will get the error

The StyleSheetTheme property cannot be set, please override the property instead.

In order to apply the StyleSheetTheme at runtime, you must override the property like so:


public override String StyleSheetTheme
{
    get { return "Simple"; }
}
Ideally, use the above code in a base page which all your pages derive from.