dotnet thoughts 

a dotnet developer's technical blog

Controls Toolbox not showing in Visual Studio 2010

Today I faced a strange problem. I am missing controls from my Visual Studio toolbox. I did only one change in Visual Studio today, I installed Microsoft Pex and Moles. And after that when I took Windows Form project, I couldn’t see any common controls in my toolbox. I tried right click and reset toolbox option, but it was also not worked.

Toolbar missing controls.

Toolbar missing controls.

After few searches I come to know I can fix this issue by deleting toolbar cache files.The cache files is available on C:\Documents and Settings\[username]\Local Settings\Application Data\Microsoft\VisualStudio\10.0.

Toolbar cache location

Toolbar cache location

In this location you can find four hidden files with TBD extension. Delete all the files; make sure you are not running any instance of Visual Studio. Visual Studio will recreate it next time when open the toolbox.

Voila ! I got it back :)

Toolbar is restored.

Toolbar is restored.

Happy Programming :)

How to check you are running as administrator using C#

If you are developing applications for Vista+, you require Administrative permissions to access system resources like FileSystem, Registry etc. Already I posted Create UAC Compatible applications in .NET. The following code will help to verify and execute code which can be run by administrator only.

private bool IsCurrentlyRunningAsAdmin()
{
    bool isAdmin = false;
    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
    if (currentIdentity != null)
    {
        WindowsPrincipal pricipal = new WindowsPrincipal(currentIdentity);
        isAdmin = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
        pricipal = null;
    }
    return isAdmin;
}

Quick introduction to SQLite with C#

Recently I am started working with SQLite Database. You can find more information about SQLite Database from the website (http://www.sqlite.org/).

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain. – From SQLite website.

You can download the System.Data.Sqlite from system.data.sqlite.org website.
Here is a sample code snippet which connects to a SQLite DB and prints all the objects in the SQLite database.

string dbName = @"C:\Sample.db3";
using (SQLiteConnection sqlConnection =
    new SQLiteConnection("Data Source=" + dbName + ";Version=3;New=True;Compress=True;"))
{
    using (SQLiteCommand sqlCommand = sqlConnection.CreateCommand())
    {
        sqlConnection.Open();
        sqlCommand.CommandText = "SELECT * FROM sqlite_master";
        using (SQLiteDataReader dataReader =
            sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
        {
            Console.WriteLine("Name : Type");
            while (dataReader.Read())
            {
                Console.WriteLine(
                    dataReader["Name"].ToString() + ":" + dataReader["Type"].ToString());
            }
        }
    }
}

Don’t forgot to Add reference of System.Data.SQLite.dll in the project.

How to store TFS credentials

In my machine the build notification tool was in system startup, and I am connected to two TFS Servers, currently team explorer / source control explorer / build notification tool doesn’t support remember password option. Because of this, I had to give the user credentials for TFS every time (My windows credentials are different from TFS credentials). Today I found a solution, which helps to store the TFS credentials in local machine; which helps to avoid the login dialog from Build notification tool as well as from the team explorer and source control explorer windows.

In Windows Vista:

  1. Go to the User Accounts Control Panel > Manage Your Network Passwords
  2. Click Add, Enter TFS server, User name and password.

In Windows XP:

  1. Go to Start > Settings > Control Panel > User Accounts > Advanced > Manage Passwords > Add
  2. Enter TFS server name, user name and password

How to associate a File Type to your application

Recently I was working with an Open source project which helps to manage SQLCE databases. Yesterday I thought of implementing a feature like associating the SDF files to my application, so that I can double click on any SDF file which will open the launch my application and opens the File in it.

Caution: Incorrectly editing the registry may severely damage your system. Back up the current version of the registry before making any changes. You should also back up any valued data on the computer.

I found various ways to do it, but the easy way I found I am sharing.

Dim SDF As RegistryKey = My.Computer.Registry.ClassesRoot. _
    CreateSubKey(".sdx")
SDF.SetValue("", "SDFUtility", RegistryValueKind.String)
Dim OpenCommand As RegistryKey =
    My.Computer.Registry.ClassesRoot.CreateSubKey _
    ("SDFUtility\Shell\Explore with SDFUtility\Command")
OpenCommand.SetValue("", String.Format("{0} ""%1""", appPath), _
                        Microsoft.Win32.RegistryValueKind.String)

And here is the screenshot

Default menu item after associating the File with application

Default menu item after associating the File with application

You can remove the association by reseting the sdf key’s value. And you can delete all the keys by calling a DeleteSubKeyTree method on the SDFUtility key to remove all the keys associated with it.

SDF = My.Computer.Registry.ClassesRoot.OpenSubKey(".sdf", True)
SDF.SetValue("", "Microsoft SQL Server Compact Edition Database File", _
                     RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.DeleteSubKeyTree("SDFUtility")

You need be Administrator do this operation, otherwise you may get an exception in Windows Vista+.

Older Posts »