Code Snippets in Visual Studio 2005
page 6 of 7
by Bilal Haidar
Average Rating: 
Views (Total / Last 10 Days): 35062/ 106

How to Build a Custom Code Snippet

In this section, I will consider a step-by-step process to show you how to create a new code snippet and add it to the Visual Studio to be accessible any time you want to use it.

The code snippet I will create will be a method that fits in the Data Access Layer of any application. This method would simply return a SqlDataReader for clients living in a certain region.

The first step in developing a code snippet is to start by configuring the Header section of the new code snippet.

Create a new text file; rename the file to SqlDataReader.snippet. Then place the following text shown in Listing 2 in the snippet file.

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets xmlns="">

  <CodeSnippet Format="1.0.0">




      <Description>Code snippet for a method to return a Sql data reader </Description>

      <Author>Bilal Haidar</Author>





The next step is to specify the replicable values inside that method. Let us state the replicable values I have come up with:

  • Method name: I will give the developer freedom to change the method name.
  • SqlConnection instance name: The developer can change the SqlConnection instance used.
  • ConnectionString Text: The developer can change the Connect string text.
  • SqlCommand instance name: The developer can change the SqlCommand instance used.
  • Query Text: The developer will change the SQL statement used to retrieve a Data Reader.
  • SqlDataReader instance name: The developer will change the SqlDataReader instance name used.

The above analysis resembles the following Declarations section which is part of the Snippet section. Each Literal defines the state of each replicable value in the emitted text by the code snippet. Listing 3 below shows the Declarations section.





          <ToolTip>Method name</ToolTip>





          <ToolTip>SqlConnection Instance Name</ToolTip>





          <ToolTip>Connection String</ToolTip>





          <ToolTip>SqlCommand Instance Name</ToolTip>





          <ToolTip>The select statement to be executed </ToolTip>

          <Default>SELECT * FROM Customers</Default>




          <ToolTip>SqlDataReader Instance Name</ToolTip>




The Code section, which is a mandatory section in the Snippet section, is shown in Listing 4 below.

      <Code Language="csharp">


public static SqlDataReader $MethodName$()


  SqlConnection $SqlConnectionInstance$ = new SqlConnection("$ConnectionString$");

  SqlCommand $SqlCommandInstance$ = new SqlCommand("$QueryText$", $SqlConnectionInstance$);


  SqlDataReader $SqlDataReaderInstance$ = $SqlCommandInstance$.ExecuteReader(CommandBehavior.CloseConnection);

  return $SqlDataReaderInstance$;







The above is a simple method that instantiates a connection, command, and executes a data reader that is returned as the output of that method.

Now, copy the above sections of the code-snippet into the SqlDataReader.snippet file created in a previous step.

In Visual Studio 2005, go to the Code Snippet Manager and select Visual C# from the drop-down list of available languages. Click on Import, select the code snippet file created, and you will be faced by the following screen:

You can select the My Code Snippets checkbox or any other checkbox and click Finish.

If you add this code snippet to your page in the same way discussed earlier, you would see something similar to the following method in Listing 5 below:

public static SqlDataReader GetAllCustomers()


  SqlConnection conn = new SqlConnection("ConnectionString");

  SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);


  SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

  return dr;


All the green highlighted values are the ones defined in the Declarations section in the code snippet file.

You can either change the default values or use your own.

View Entire Article

User Comments

Title: developer   
Name: sunil
Date: 2009-10-27 9:23:04 AM
very nice exmpl
Title: developer   
Name: nanda
Date: 2007-08-06 2:00:31 AM
nice article..
Title: Developer   
Name: Stefan Leoni
Date: 2006-12-13 7:18:51 AM
concise. Excactly what i was looking for.

Title: Re:   
Name: Bilal Haidar
Date: 2006-01-20 3:38:15 PM
You can now, discuss, comment, and ask me questions related to this article at this forum:

Title: Solution Developer   
Name: Mostafa
Date: 2006-01-07 3:05:53 AM
Good Article.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-24 12:09:57 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search