Monday, July 23, 2007

Code Snippets in VS 2005

Code Snippets is a really cool feature in microsoft visual studio 2005.It enables you to store pieces of code to use in a later time givinning you the chance to store, share, publish and reuse that code.The idea is simple you store your code in an xml file with a predefined scheme and save it with .snippet extention.

Visual studio Itself comes with many code snippet written for you and ready to use.You can see all code snippets from the Code Snippets Manager of the Tools menu where you can add your own folders of code snippets.
In these steps I'll show you how to create code snippets in visual studio and introduce you to the schema and later on how to use snippets from visual studio

Crate a code snippet:

1- Add an xml file from add new item menu.
2- change the extension from .xml to .snippet
3- Under the processing instruction line of the xml file (which is the first line containg the version) write
4- you will find the schema craeted for you.all you need to understand It and replace the place holders with your own.

Understanding The Scheme
The xml scheme for code snippet is very easy:

The root elment is CodeSnippet which contains a version an a namespace. This tag contains two main tags (elements) which we will do our changes in, Header and Snippet.

Header
contains the following elements:

Title
The name of the element.

Author
The author of the code (may be your company name or your name)

Shortcut
The shortcut that you will use to reach your snipppet faster by typing your shortcut and press TAB key twice.

Description
The help alt text that will appear when user select the snippet.

SnippetTypes
Indicates how visual studio iserts the code snippet, at the cursor or surrounding a selected code.

Snippet
contains elements that includes the actual code

Imports
Imports the specified namespace when the code snippet is inserted.

Declarations
Contains Literal elements which acts as varables for the place holders used in your code snippet.
Each Literal contains ID which is the placeholder variable,Default which is the default value for that placeholder and a ToolTip which is the alt help text for that place holder.

Code
That's where our code goes.It has a lnguage attribute to specift the language which may be C#,VB,JScript or XML.

[CDATA]
here you can put your code and place the Literals ($placeholders$) between $ dollar signs or any other character you specify in the delimiter attribute of the Code element.

here is a print screen of default Scheme:




Now you have created your Code Snippet, How can you use It in Visual Studio:
1-Save your Code snippet file in a folder.
2-From Tools select Code Snippet Manager.
3-In Code Snippet Manager window choose your language and then click Add button and choose your folder.

Like that:







4-Whenever you want to use your piece of code you have two ways:

Right click your mouse in the code window where you want your code, select insert snippet , choose your folder and then your code which named as you put in the title element.



OR

write the shortcut(as specified in the shortcut element of the scheme) of your snippet and press
TAB key twice.

Visual studio comes with many snippets ready made for you like if statement.
Try It out,write down an if and then Press TAB twice and you will see.

And That's It...
Enjoy...

Friday, July 20, 2007

Sql Caching in .Net 2.0

Caching has always been a powerfull feature in Asp.Net, However in Asp.Net 2.0 It's even more powerfull.Asp.Net 2.0 has different kinds of Caching, One of most interest is SqlCaching which enables you cach your pages depending on changes in database Not only on page level but also on SqlDataSource Level.Am gonna take you in a simple steps on how to configure an use this powerfull feature and put you on the road.

In the examples I used the popular Northwind database and two of Its tables "Categories and Products".

To use SqlCaching you need to play in Three places:

1- Your DataBase.
2- Your web.config.
3- your Page ".aspx" (Note that you can use sqlcaching from your .net code and thus you don't need to configure your web.config nor your .aspx but this works only with sqlservers 2005 cause It uses Notification services.).

Configuring DataBase

To configure you database you will use the aspnet_regsql.exe utility that comes with .net sdk and enables you to install many asp.net 2.0 features like membership, sessionstate and Caching (use aspnet_regsql.exe /? to list all options of that tool)

1-from your start menu open Programms/VisualStudio 2005/VisualStudio Tools/VisualStudio Command Prompt.

2-To enable your database"Northwind " for caching use this aspnet_regsql.exe -E -d Northwind -ed
where -E means use windows authentication when connecting to the database server, -d is the database switch and -ed means Enable database Caching

Then write this
aspnet_regsql -E -d Northwind -t Categories -et
here -et means Enable table Caching.

Now if you go to your Northwind database you will find a new table called AspNet_SqlCacheTablesForChangeNotification along with multiple stored Procudures and a Trigger on the Categories table that will be triggered each time a change happen to that table which in turn will invalidate the cache.

Configure wen.config

1- Add a connectionString to the ConnectionStrings section like that





2- Add Caching section to your web.config System.web section like that



In the caching section we added a sqlCacheDependency enabled = true and poll time to one second since it's in milleseconds."Poll time determines how often will asp.net go to sql to check if the data has been changed. Inside the sqlCacheDependency is a databases section that you use to define the database for this sql caching "yopu can provide as many as you want since the name is different".

Configure you page

Add the outputCache directive in your page as follows



Note The SqlDependency attribute value consists of name of SqlDependency from web.config, colon and then the table name on which caching is work. and that's It in Its simplest implementation. Note that I didn't intended to cover everything here, It's only a starting point for you on how to use It and there are many other features and tricks on how to use SqlCaching in ASP.Net 2.0.

The main idea is that:

when you run the aspnet_regsql tool It creates a table with 3 columns : tablename,notificationcreated and changeID and some stored procedures and a trigger on the table you provided gor caching. Whenever a change occur in the table the trigger is fired and update the changeID field in the corresponding record for that table in the generated table. when you request a page that sqlCache enabled for, It check the poll time in web.config and if It expired it goto database and check the changeID field and if its value was changes it invalidates the cache. Understanding that infrastructure gives you flexibility on using this fetaure like making cache based on subsets of data rather than the whole table by making your own trigger and inserting the record yorself in the generated table.

Enjoy...

Sunday, July 8, 2007

Interview puzzle

Many people who had several interviews may have faced this puzzle as I faced It and I thought of sharing the solution with you.


Puzzle: We have 8 balls with the same size.7 balls hve the same weight and the remaining one is heavier.Having a balance, how can you determine the heavier ball with only 2 weightings.


Answer:
take 6 balls and weight them (3 balls in each side) "First weighting".

If the two sides are equal then none of the 6 balls is the heavier:
- Weight the remaing 2 balls "second weighting" so you can get the heavier one.

Else If the two sides are not equal so the heavier ball will be in the heavier side and that makes 3 remaining balls with equal probability of being heavier than each other.

-Take one in hand and weight the other two "second weighting":
If they are equal so the hevier one is in your hand.
else sure the balance will tell youuuuuuuuuuuu.

Enjoy..........