Hello world! Now on WordPress

I have moved my blog from http://spaces.live.com/mrityunjaya to http://mrityunjayakumar.wordpress.com.

Posted in Uncategorized

Dual boot with windows7 (64 bit) and windows vista (32 bit)

With the release of windows7, I opted to fresh install the windows7 64bit OS at my home PC. The installation was smooth and within 30 minutes I was running 64bit Windows7. I installed other important software for my regular activities and all ran fine with only exception that 32 bit software entries were not displayed in the context menus.

I later tried to see whether 64bit and 32bit OS can co-exist or not as dual boot. Since I already had the Vista(32bit) SP1 with me, I fresh installed it as dual boot on separate disk, hoping it should install fine without any issue.

To my surprise, I didn’t faced any issues while install. You will see the upgrade option as grayed out, because the Windows7 version is higher than the Vista version. Also you can’t upgrade Window7 64bit to Vista 32 bit.  Overall, the installation went smoothly and after install I got Windows Vista as default OS in boot menu. This you can change back to Windows 7 using BCEdit tool.

There was no problem after installation of both 64 bit and 32bit OS as dual boot. Also order of installation (i.e. first windows7-64bit and then windows vista-32bit) also didn’t created any issue, as recommendation is to always install the lower version OS first and then higher version.
Posted in Dual Boot

How to Create your own Visual Studio 2005/2008 code snippets

Overview of Code Snippets

Code snippets is new feature added in visual studio 2005, which allows to use a pre-authored piece of code. This helps in fast development and re-usability of standard pre-authored piece of code throughout your application.

Microsoft ships set of pre-authored code-snippets with visual studio 2005. you can also download many snippets from Microsoft site. These snippets provide many re-usable pre-authored piece of code that can directly inserted in our application. But many time you will require to create your own snippet as per you application requirement. The good news is that using IntelliSense Code Snippet XML schema elements you can create your own code snippets. Once code snippet is created you can use Visual Studio Code Snippet Manger and add them to the code snippets that Visual Studio 2005 already includes.

Code snippets are xml files that uses code snippet xml schema elements. You can use the IntelliSense Code Snippet XML schema to create your own Visual Basic, Visual C#, Visual J#, or XML code snippets.

Sample code

In this tutorial I will show you how to create a simple code snippet.

Snippet definition : I want to create a simple code snippet for C# language that read a SQL server database table content using data reader class.

Following is the code that I will convert into snippet. The purpose of the code is to read a table from SQL server database and store results into reader object. You will need to change the connection string and query as per your requirement in this code.

Sample Code that will be converted into snippet.
	//Creates instance of sql connection
	SqlConnection conn=new SqlConnection("connectionString");
	SqlDataReader rdr=null;
	try
	{
		//create new sql command object
		SqlCommand cmd = new SqlCommand("Sql query", conn);
		cmd.CommandType = CommandType.Text;

		if ( conn.State != ConnectionState.Open )
			conn.Open();
		rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
				
		//Check if reader has any records within it.
		if ( rdr.HasRows )
		{
			while ( rdr.Read() )
			{ 
				//Read table column information from reader
			}
			rdr.Close();
		}
	}
	catch(SqlException ex)
	{
		//Handle Database related exception here
	}
	catch(Exception ex)
	{
		//Handle General exception here
	}
	finally
	{
		//Close reader
		if ( rdr != null && rdr.IsClosed == false )				
			rdr.Close();

		//Dispose reader
		rdr.Dispose();
				
		//Close Connection
		if ( conn != null && conn.State != ConnectionState.Closed )
			conn.Close();	
				
		//Dispose connection if you don't require
			conn.Dispose();
	}

Following are the steps to create code snippet (.snippet) file

  1. On the File menu, click New and then click File.
  2. Click XML File and then click Open.
  3. On the File menu, click Save.
  4. In the Save as type box, select All Files (*.*).
  5. In the File name box, enter a file name with the .snippet file name extension.
  6. Click Save.

Now xml file is created and now you need to put xml code to create your own code snippet. To avoid any errors in your code snippent you can use this code

Sample Snippet Code.
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<Header>
		<Title>title</Title>
		<Author>author</Author>
		<Shortcut>shortcut</Shortcut>
		<Description>description</Description>
		<SnippetTypes>
			<SnippetType>SurroundsWith</SnippetType>
			<SnippetType>Expansion</SnippetType>
		</SnippetTypes>
	</Header>
	<Snippet>		
		<Declarations>
			<Literal>
				<ID>literal id</ID>
				<Default>literal value</Default>
			</Literal>
		</Declarations>
		<Code Language="CSharp">
			<![CDATA[
				//Write your snippet code here
			]]>
		</Code>
	</Snippet>
</CodeSnippet>

Following is the description of the code snippet xml schema elements we have used within our snippet code

  • CodeSnippet : Allows you to specify a heading and multiple IntelliSense Code Snippets, which you can insert into Visual Studio 2005 code files.
    • Header : Specifies general information about the IntelliSense Code Snippet.
      • Title : Specifies the title for the code snippet.
      • Author : Specifies the name of the snippet author.
      • Shortcut : Specifies the shortcut text used to insert the snippet.
      • Description : Specifies descriptive information about the contents of an IntelliSense Code Snippet.
      • SnippetTypes : Groups individual SnippetType elements..
        • SnippetType : Specifies how Visual Studio inserts the code snippet
    • Snippet : Specifies the references, imports, declarations, and code for the code snippet.
      • Declarations : Specifies the literals and objects that make up the parts of a code snippet that you can edit. The literals/object id in declarations section can be used in code and must prefixed and suffixed by $. For ex. if a literal is "SQL" then in the code section you have to use it as $SQL$.
      • Code : Provides a container the short code blocks of IntelliSense Code Snippets.

Code Snippet to read table using data reader

Save the following following content in your code snippet file.

Code Snippet File : ReadTableUsingDataReader.snippet
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<Header>
		<Title>Read table content using SQL Data Reader</Title>
		<Author>Mrityunjaya Kumar</Author>
		<Shortcut>ReadTableUsingDataReader</Shortcut>
		<Description>
			The purpose of the code is to read a table from SQL server database 
			and store results into reader object. You will need to change the 
			connection string and query as per your requirement in this code.
		</Description>
		<SnippetTypes>
			<SnippetType>SurroundsWith</SnippetType>
			<SnippetType>Expansion</SnippetType>
		</SnippetTypes>
	</Header>
	<Snippet>		
		<Declarations>
			<Literal>
				<ID>ConnectionString</ID>
				<Default>Server=(local);Data Source=(local);Initial Catalog= ;UserId=;Password=;</Default>
			</Literal>
			<Literal>
				<ID>Query</ID>
				<Default>Select * from table</Default>
			</Literal>
		</Declarations>
		<Code Language="CSharp">
			<![CDATA[
			//Creates instance of sql connection
			SqlConnection conn=new SqlConnection("$ConnectionString$");
			SqlDataReader rdr=null;

			try
			{
				//create new sql command object
				SqlCommand cmd = new SqlCommand("$Query$", conn);
				cmd.CommandType = CommandType.Text;

				if ( conn.State != ConnectionState.Open )
					conn.Open();
				rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				//Check if reader has any records within it.
				if ( rdr.HasRows )
				{
					while ( rdr.Read() )
					{ 
						//Read table column information from reader
					}
					rdr.Close();
				}
			}
			catch(SqlException ex)
			{
				//Handle Database related exception here
			}
			catch(Exception ex)
			{
				//Handle General exception here
			}
			finally
			{
				//Close reader
				if ( rdr != null && rdr.IsClosed == false ) 
				rdr.Close();

				//Dispose reader
				rdr.Dispose();
				
				//Close Connection
				if ( conn != null && conn.State != ConnectionState.Closed )
				conn.Close(); 
				
				//Dispose connection if you don't require
				conn.Dispose();
			}
			]]>
		</Code>
	</Snippet>
</CodeSnippet>

Adding Code Snippet to Visual Studio

After saving the .snippet file on disk, now we have to add our snippet into the visual studio so that we can use it in our application. For adding custom snippet in the list of existing Visual Studio snippets

  1. Go to Tools > Code snippet Manager or press Ctrl+K, Ctrl+B.
  2. It will display code snippet manager window. select "import" button and select the .snippet file you have created.
  3. after selecting the file code snippet manager will display the "Import Code Snippet" window where you can add the snippet to specific location. We have created code snippet to be used in Visual C# application, so select Visual C#.
  4. Click Finish and your code snippet is available to be used in your application

To use you code snippet, right click inside the method and select "Insert Snippet". Select "Visual C#" and it will display list of all the Visual C# snippets. Find your snippet by the title you have assigned in snippet file or use short cut to find your snippet. The snippent will be added to your cursor location.

Posted in Visual Studio 2005/2008 | 1 Comment

Creating and Managing help in 1.x and 2.x format

 
While developing my first .net project, I was asked to create compiled HTML help (.CHM) also. I searched for the tool on internet and got a tool on microsoft site i.e. HTML help workshop. The way it generated the help actually fascinated me. Later on I used this tool in many of the project to create help.
 
After the launch of Visual Studio .net (i.e. VS2002), Microsoft changed the help format and it was known and Help 2.x format. VS 2002 onward versions used code comments in the code itself and compiler generates the xml document file that was used by help generation tools to create 1.x (.chm) and 2.x format help.
 
I thought putting information about various tools available for help generation at one place will be a definite help for anybody interested in help generation and for me also, as i don’t have to recollect it again and again :-).
 
There are few tools on windows platform to create Help in 1.x and 2.x format.
 
1. HTML help workshop : This tool creates compiled html help (Help 1.x) from the html files. Download url is http://www.microsoft.com/downloads/details.aspx?FamilyID=00535334-c8a6-452f-9aa0-d597d16580cc&DisplayLang=en
 
2. NDoc : NDoc is an Open Source help generation tool, that is used to generate help in 1.x and 2.x (for VS 2003) format. The download url is http://ndoc.sourceforge.net/
 
The version that support VS2005 style help is still in alpha. It can be downloaded from http://www.kynosarges.de/NDoc.html.
 
3. Visual Studio.net Help Integration Kit 2003 : The Help Integration Kit provides documentation and samples that show how to integrate new Help content into the Visual Studio development environment. This version of the integration kit targets Visual Studio .NET 2003. Download url is http://www.microsoft.com/downloads/details.aspx?familyid=CE1B26DC-D6AF-42A1-A9A4-88C4EB456D87&displaylang=en
 
4. Sandcastle : The sandcastle is new help generation engine for .net that create MSDN style help (2.x) by reflecting over the source assemblies and optionally integrating XML Documentation Comments. SandCastle doesn’t provide any UI for help generation. The download url for Sandcastle October CTP is http://www.microsoft.com/downloads/details.aspx?FamilyId=E82EA71D-DA89-42EE-A715-696E3A4873B2&displaylang=en.
 
For generating help using Sandcastle, you will require Html Help Compiler (hhc.exe) for 1.x help and MS help compiler for 2.x help. The 2.x help compiler comes with the Visual Studio 2005 SDK 4.0 (http://www.microsoft.com/downloads/details.aspx?familyid=51A5C65B-C020-4E08-8AC0-3EB9C06996F4&displaylang=en).
 
5. SandCastle Help File Builder : This is nDoc style GUI and Console based tool that uses SandCastle as help generation engine to generate MSDN style 1.x, 2.x and html help. The tool can be downloaded from http://www.codeplex.com/SHFB.
 
6. DocProject : DocProject drives the SandCastle help generation tools using the power of Visual Studio 2005/2008 and MSBuild. The tool can be downloaded from http://www.codeplex.com/DocProject.
 
Also there is SandCastle Extension project ( http://www.codeplex.com/sandextensions ) on codeplex that adds some missing features in the original sandcastle distribution and Visual Studio 2005 Addin suite (http://www.codeplex.com/SandcastleAddIn) that enhances the help generation experience.
 
7. Innovasys HelpStudio Lite : This is MSDN style help 2.x generation tool that comes free with Visual Studio 2005 SDK 4.0. It also provide tool to integrate the 2.x help with the MSDN collection. For further information visit the site http://www.innovasys.com/products/hs3/overview.aspx.
 
8. SandCastleGUI : This is a free GUI for the SandCastle to generate help for .net. The tool can be downloaded from http://www.inchl.nl/SandcastleGUI/.
 
9. Far HTML and H2Reg : These are shareware tools for generating help in 1.x and 2.x format. Far HTML contains help viewer where you can view 2.x help without integrating it with the document explorer. H2Reg is alternative to help integration using MSI installer. These tools can be downloaded from http://www.helpware.net/. This site is a good resource for help generation faqs and information.
 
I think this is enough for the time being. I will update on other help generation tool once I encounter them.
 
Posted in Help Generation | Leave a comment

SnippetBuilder 1.0 alpha launched

I always liked Visual Studio 2005/2008 code snippets feature. Visual Studio provides easy way of using the code snippet, but not the same easy way of creating it. So, in my leisure time I created my own code snippet builder tool. I have published this tool on codeplex. The tool can be downloaded from  http://www.codeplex.com/SnippetBuilder.
 
Although it is still the first release, it allows to create and save new code snippets and view and modify the existing code snippets, that comes with Visual Studio. Here are some screen shots of the tool.
 
 
 
 
Currently working on its new feature sets and documentation to make it more useful and productive for the developers.
Posted in Visual Studio 2005/2008 | Leave a comment

Visual Studio 2008 is now no longer beta

Visual Studio 2008 is now no longer beta. Microsoft has released new Visual Studio 2008 express editions with lot of additional developer tools and ready to use libraries.
 
Download all this exiciting stuffs from www.microsoft.com/express.
 
Check out the new Coding4Fun developer tookits and game developers can download the new Game Development kit (GDK). It now time to enjoy new degree of fun with cool tools and technologies integrated within the Visual Studio 2008.
Posted in Microsoft.net | Leave a comment

Invalid FORMATETC structure (Exception from HRESULT: 0×80040064 (DV_E_FORMATETC))

I was developing some custom control and started getting this problem suddenly. While trying to figure it out, I realized that it was [TooboxItemAttribute] that was causing problem.
 
In my case, I was developing one base WebControl and one child webcontrol derived from base. I didn’t wanted to display the base webcontrol in the toolbox, so I set the [TooboxItem(false)] in the base webcontrol. In child webcontrol it was set to [ToolboxItem(true)]. After setting this ToolboxItemAttribute class, I started facing the problem and I was not able to drag-n-drop the control over the designer.
 
After I removed the [TooBoxItemAttribute] from both the webcontrol classes, the problem was gone and I was again able to drag-n-drop the controls.
 
It’s really pethatic that there is no help available for this error and error message doesn’t reveal any information to crack this problem.
Posted in .net (control development) | Leave a comment