Thursday, June 24, 2010

Dynamically Loading Master Pages

Master Pages give you the ability to define a master page layout and look that is used throughout a site to give a consistent look & feel to all pages. Any updates or changes to the look & feel of the site is done in only one place - the Master Page. Something that might be useful is the ability to dynamically load a master page at runtime, for example based on user preferences or to implement a simple skinning ability for a site framework that might host multiple different sites.

The master page can be selected for a page easily at design-time. But assigning a master page at run-time can only be done in the OnPreInit event since the rendering of the page with the master page occurs prior to the Init event. However, in the case where you want to load the master page dynamically, for example from a database entry based on a user's preference or something, you don't want to add code to the OnPreInit event on every page in the site. It is easy enough to add that code to a base page class that you can inherit from for you pages in the site.

public class DynamicPage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
string masterfile = getMasterPageFromDatabase();
if (!masterfile.Equals(string.Empty))
{
base.MasterPageFile = masterfile;
}
base.OnPreInit(e);
}
}


Now, all you have to do is inherit from that class for all the pages in the site. You can just add it into the web.config and all pages will automatically inherit from it. Doesn't matter if it is a page in the project that you have already, or a new page you add later, it will know to inherit from the DynamicPage class. You add the following to the web.config:



<system.web>
<pages pageBaseType="DynamicPage" />
<!-- ...rest of system.web items... -->
</system.web>

Wednesday, June 23, 2010

How to: Adding Uninstall start menu item to your .NET deployment project

Its pretty simple. Follow the steps to achieve this.

  • Add the following code snippet  to your projects main method before InitializeComponent();
    string[] arguments = Environment.GetCommandLineArgs();
    foreach (string argument in arguments)
    {
       string[] parameters = argument.Split('=');
       if (parameters[0].ToLower() == "/u")
       {
         string productCode = parameters[1];
         string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
         Process proc = new Process();
         proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
         proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
         proc.Start();
       }
    }



    • Now go to your deployment project and go to the file system editor and right click on File System on Target Machine and Add Special Folder with name User's Programs Menu.


    • Add an additional shortcut to your primary output project and name it Uninstall.


    • Right Click  Shortcut to Set the Arguments property to /u=[ProductCode].


    • Now Build your Deployment setup project. Your are ready to go.


    Deployment project will replace [ProductCode] in the Arguments property with the actual installer project's ProductCode GUID value. Your program will see the /u={Actual ProductCode} argument and pass it to msiexec.exe. If you want the product to remove only, replace the "/i " with "/x ".

    Tuesday, June 22, 2010

    How to: Check whether SQL login exists?

    Try some thing like this.

    DECLARE @SqlStatement NVARCHAR(4000)
    DECLARE @loginName VARCHAR (100)
    SELECT @loginName = 'nagasai'
    IF NOT EXISTS (SELECT loginname FROM master.dbo.syslogins WHERE NAME = @loginName)
    BEGIN
    SET @SqlStatement = 'CREATE LOGIN [' + @loginName + '] WITH PASSWORD=N''angel83'',DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]'
    EXEC sp_executesql @SqlStatement
    END
    ELSE
    BEGIN
    PRINT 'login name ' + @loginName + ' already exists'
    END

    How to: Create login as sysadmin from T-SQL

    Here is the T-SQL code snippet that creates login with sysadmin permissions.

    CREATE LOGIN [nagasai] WITH PASSWORD=N'angel83', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=ON, CHECK_POLICY=ON
    GO
    EXEC master..sp_addsrvrolemember @loginame = N'nagasai', @rolename = N'sysadmin'

    Friday, June 18, 2010

    How do I debug a custom action/installer class?

    Here is how you can do debug custom action.  You can try using one of the following methods:

    • Add a call in your code to System.Diagnostics.Debugger.Launch. This method opens Just-In-Time debugging and enables you to attach a new debugger to your code.
    • Add a call in your code to MessageBox.Show("Debug Me"). When the message box is shown, use Visual Studio to attach to the MessageBox process. Then add breaks (for Visual C# projects) or stops (for Visual Basic projects) in the code.
    • Set your debugging preferences to start %windir%\Microsoft.net\Framework\version\InstallUtil.exe as an external program on the Debug Page of the Project Designer. The name of the custom action assembly is the command line arguments. When you press F5, you hit your breakpoint. InstallUtil.exe will run your custom actions just as MSI does.

    Life was difficult until i find this solution from MSDN.