Google Search

Google
 

Wednesday, June 4, 2008

Code Snippet in asp.net 2.0

What is a “Code Snippet”?
A: A Code Snippet is a reusable block of code. Unlike a static copy-and-paste approach, code snippets allow for dynamic instances of code
by allowing placeholders into mini templates of code. Code snippets are a new feature of Visual Studio 2005.
Q: How do I use a Code Snippet?
A: In the Visual Studio 2005 code editor, you can invoke snippets with [ctrl][k][x] keyboard shortcut. IntelliSense will then display a
context menu displaying the available code snippets to choose. However, most code snippets are available without using the keyboard
shorcut. If you know the shortcut name of the code snippet, simply type the name and press tab to invoke.
Now we will see how we can use snippets here.I will write a small snippet for disconneceted data access with dataset.


1) Open nottepad.
2) Type the code in it and save it as sample.snippet.
http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>

DatasetCode snippet for Disconnected data accessBharath RadhekrishnaExpansionSurroundsWith

3) After saving this. Go to visual studio 2005. Click Tools > CodeSnippets manager or ctrl+K, ctrl+B.and add this snippet to library.
4) While you are coding right click the mouse. You will see an option of “insert snippet”. when you click it will tell usto insert the snippet we like. For example if take the above snippet it will be displayed with the name as:“Disconnected Data architecture”.If we click it will display the code as:
SqlConnection cn = new SqlConnection();string selquery = ;SqlDataAdapter da = new SqlDataAdapter(selquery, cn);DataSet ds = new DataSet();da.Fill(ds);So everytime it is not necessary for us to write whole sentence or connections.It will reduce our burden of writing commonly usedcode everytime.

No comments: