Displaying all warnings…

Posted in Microsoft.NET on February 9, 2007 by vasudevan

VS.NET 2005 defaults to 200 warnings in the Error List view when a project is compiled. The VS.NET team decided with this number to improve performance of the IDE.  However, it is better to view all warnings at once to enable a developer to prioritize on fixing each of the warning.

To override the 200 warning limit, do this: Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Setup\EDev\CodeAnalysisErrorListViolationLimit and increase the number to something bigger (hex value).

It is a general practice for most of the developers to ignore all warnings and move their app to prod which I feel is not good. It is always better to fix all the warnings and the best approach is to to treat all warnings as errors and fortunately, VS.NET has a setting that can do the same. So, once this setting is enforced in VS.NET, your project will never compile successfully until all warnings are fixed.

Detecting the default browser…

Posted in Microsoft.NET on December 21, 2006 by vasudevan

On Oct 18, I wrote a article on showing hyperlinks from a WinForms 2.0 application. In that article, I also mentioned that I will be writing a article to check the default browser instead of hard coding the .exe file for Internet Explorer. Now this article discusses that technique.


private enum ShowCommands
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}

[DllImport(“shell32.dll”)]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);

IntPtr ptr = IntPtr.Zero;
ptr = ShellExecute(IntPtr.Zero, “open”, “http://channel9.msdn.com”, “”, “”, ShowCommands.SW_NORMAL);

What is happening here is ShellExecute Win32 API call is reading the registry entry at HKEY_CLASSES_ROOT\HTTP\shell\open\command. If you would like to delve under the hood on what is happening in the registry try Regmon. We can make this code more robust by checking whether the entry at that registry location is empty and ask the user to set the default browser in the registry which is pretty easy to do.

The power of IHttpModule…

Posted in Microsoft.NET on December 13, 2006 by vasudevan

I have been reading Marco Bellinaso’s ASP.NET 2.0 Website Programming Problem – Design – Solution. To begin with, the topic that I was interested in was the selectable theme technique explained in Chapter 2 (pages 38 – 41). I thought about his approach and I should admit that the idea was very good, but I was still not convinced and decided to code it using a more efficient approach using IHttpModule. IHttpModule is powerful and efficient enough and this is how I achieved the same functionality:

1. In my approach, there is no need for the ThemeSelector user control. I added the DropDownlist control in the Master page itself. Everybody knows that there is no Page_PreInit() event for Master pages. But, to set a user selectable theme at runtime, Page_PreInit() is the only event that is pluggable for theme selection.
2. On the Page_Load of the master page, code as shown in page 38.
3. On page 39 of his book, there is a bug in the GetThemes() method that returns themes string array. The variable themes is unreachable. So, modify your code to fix that.
4. Create a new regular folder under App_Code by name Modules.
5. Create a new class and name it ThemeModule.cs and copy the following code by overwriting all of the code in your class:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

public class ThemeModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.PreRequestHandlerExecute +=new EventHandler(application_PreRequestHandlerExecute);
}

public void Dispose()
{
}

private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.EnableTheming = true;
page.PreInit +=new EventHandler(page_PreInit);
}
}
public void page_PreInit(object sender, EventArgs e)
{
Page page = (Page)sender;
if (page != null)
{
if (HttpContext.Current.Request.Form["__EVENTTARGET"] == "ctl00$ddlThemes")
{
page.Theme = (string)HttpContext.Current.Request.Form["ctl00$ddlThemes"];
HttpContext.Current.Session["CurrentTheme"] = page.Theme;
}
else
{
if (HttpContext.Current.Session["CurrentTheme"] != null)
{
page.Theme = (string)HttpContext.Current.Session["CurrentTheme"];
}
else
{
PagesSection pagesSection = (PagesSection)ConfigurationManager.GetSection("system.web/pages");
page.Theme = pagesSection.Theme;
HttpContext.Current.Session["CurrentTheme"] = page.Theme;
}
}
}
}
}

All the code does is to check whether the page is requested for the first time. If it is, it reads web.config for the pages section and gets the theme entry and stores the value in a session variable. If the same user requests the same page or some other page, the theme will be loaded from the session. Lastly, if the same user selects a different theme from the dropdownlist, the code checks for postback information by reading the __EVENTTARGET and reading the value of the control’s unique id (ctl00$ddlThemes) and rewrites the session variable.

This code is definitely more efficient than coding a base page that derives from the System.Web.UI.Page and having to change it for every new page you create. Also, this approach is completely transparent and there is nothing that the other developer should do to use this.

Btw, don’t forget to configure your web.config like this:

<httpModules><add name =ThemeModule type=ThemeModule/></

httpModules><pages theme=TemplateMonster masterPageFile=~/Template.master />

ObjectDataSource, GridView and .NET Web Services

Posted in Microsoft.NET on December 11, 2006 by vasudevan

To the best of my knowledge, most of the sites that talk about GridView and business objects in .NET 2.0 explain the steps involved in creating those business objects and using ObjectDataSource control to bind to GridView control. But, what if you have to interface through web services and cannot access the business object directly? Its pretty easy. The following are the steps involved in binding web services to ObjectDataSoure control.

1. Add a web reference to the web service.
2. On a ASP.NET web form, drag GridView control and click on the Choose Data Source from the list of available entries.
3. Select Object from the list of data source types.
4. On the next page, the list of business objects shows all public methods from the web service. You should be careful to select the main name for the webservice. (e.g) localhost.MyWebService
5. On clicking Next, choose a method that you would like to run.
6. If you would like to enable paging, this is the time to do so.
7. Press F5 to check your results.

Binding Databound controls to Enum

Posted in Microsoft.NET on October 10, 2006 by vasudevan

The other day I was curious to bind a static enum list to any databound control in .NET 2.0. The enum might be part of your business object in the middle tier. I did some research on this and found that is pretty easy and intuitive. Let’s see a example:


public enum ApartmentTypes
{
Studio,
OneBedroom = 1,
TwoBedroom = 2,
ThreeBedroom = 3
}

All user defined enums extend System.Enum class. So, to bind DropDownList control to ApartmentTypes enum, the code looks like this:


if (!Page.IsPostBack)
{
DropDownList1.DataSource = Enum.GetNames(typeof(ApartmentTypes));
DropDownList1.DataBind();
}

This code will bind to the enum directly and looking at the page source reveals that both the text and value for each entry in the Dropdownlist control is the same. Since no two entries will have the same entry, this shouldn’t cause any trouble. But, ideally the value should have the actual enum values and not the text to enable us to do more processing when doing a postback. To get the value for the entry selected in the dropdownlist, we should use the Parse method in the Enum class.


int value = (int)Enum.Parse(typeof(ApartmentTypes), DropDownList1.SelectedItem.Text);

This is the final code:

if (!Page.IsPostBack)
{
DropDownList1.DataSource = Enum.GetNames(typeof(ApartmentTypes));
DropDownList1.DataBind();
}
else
{
Response.Output.WriteLine((int)Enum.Parse(typeof(ApartmentTypes), DropDownList1.SelectedItem.Text));
}

Windows Media Player 6.4 advantages

Posted in Windows on October 2, 2006 by vasudevan

Windows Media Player 6.4 (albeit pretty old) still has big advantages over the new versions of WMPs. The major one is that it supports seeking a media file. Try the same media file on the new versions, seeking may not work. But, the WMP6.4 install program will not allow you to execute it on XP. It spits this error when  you try to run the installer:


This version of Windows Media Player is not currently supported on Microsoft Windows 2000. A version supporting Windows 2000 will be made available on www.microsoft.com at a future date.

To force WMP6.4 to run on XP, open the installer file “mpfull.exe” in WinRAR, extract all the files to a folder you want and run “mplayer2.exe”. It should open up fine. Go this link to get WMP6.4: WMP6.4

Also, another advantage with this version is smaller footprint when compared to the new versions.

HyperlinkField doesn’t work in GridView control

Posted in Microsoft.NET on September 29, 2006 by vasudevan

In my previous project, I had to display e-mail addresses for each row in the GridView as a hyperlink to enable users to click on the link which will take them to compose e-mail in OutLook. I was taken aback to see that after the page was rendered, the link didn’t show but the e-mail address was shown as a static text. Bummer!!! I did some research on why ASP.NET 2.0 was behaving like that and found out that the src/href elements were disabled because the data might be malicious. But after few hours of trying, I found a workaround: Instead of using Hyperlink field, we can use a BoundField and set the DataFormatString property.

<a href=mailto:{0}>{0}</a> and this works perfectly. Here {0} is your datafield column from your datasource which has the e-mail address.