dotnet thoughts 

a dotnet developer's technical blog

How to delete all Indexes of SQLite database

Here is the code snippet which will delete all the Indexes in SQLite Database.

PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'index';
PRAGMA writable_schema = 0;

You can also use Drop Index command for deleting Indexes. The snippet can be used to delete Tables also, instead of Index use Table.

How to show confirm dialog before user closing browser tab or window

Recently in ASP.Net forums, I found a question like how to show an alert box before user closing or leaving browser window or tab. You can find the similar dialog, in StackOverflow.com , when you are started answering a question and then trying to close window / tab or navigating from it without submitting the answer. And here is the snippet which will help you to do this.

function closeIt()
{
  return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;

This will display a dialog like this

Navigation confirmation dialog

Navigation confirmation dialog

Note: This confirm dialog will also appear when you start navigating from the page to another also.

Windows 8 Consumer Preview and Visual Studio 11 Beta available for download

Microsoft released Windows 8 Consumer Preview and Visual Studio 11 Beta yesterday (29th Feb 2012).

Windows 8

Windows 8

Windows 8 is the codename for the upcoming version of Microsoft Windows that follows Windows 7. It features a new Metro-style interface that is designed for touchscreen, mouse, keyboard, and pen input. It also adds support for the ARM processor architecture in addition to the previously supported x86 microprocessors from Intel and AMD.

You can download it in following links

How to ignore the Alt-F4 key combination in C#

The Alt-F4 is a windows global shortcut which closes an active window. For some reason you may not be fond of this behavior. After doing some digging around, I finally found one method to ignore this request by overriding the ProcessDialogKey() method of Windows Form.

Here is the simple snippet which will ignore the Alt + F4 combination.

protected override bool ProcessDialogKey(Keys keyData)
{
    if (keyData == (Keys.Alt | Keys.F4))
    {
        return true;
    }
    return base.ProcessDialogKey(keyData);
}

Happy Programming. :)

How to programmatically start and stop windows services using C#

Today I got chance a to work around windows services. And I found ServiceController class, which helps to start, stop and get status on any windows service installed. Here is the code snippet which will get status of MySql service installed, and if it is not started, starts it. After two minutes again stops it.

const string serviceName = "MySql";
var serviceController = new ServiceController(serviceName);
if (serviceController.Status != ServiceControllerStatus.Running)
{
    serviceController.Start();
    serviceController.WaitForStatus(ServiceControllerStatus.Running);
}

Thread.Sleep(2 * 60 * 1000); //Sleeping

if (serviceController.Status != ServiceControllerStatus.Stopped)
{
    serviceController.Stop();
    serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}

Note: This will not work, if your service is in disabled state. You will get an Invalid Operation exception, with message like this Cannot start service MySQL on computer ‘.’.

Happy Programming :)

« Newer PostsOlder Posts »