Monday, January 19, 2009

WCF Streaming - Couple of blogs to read

Recently I was building a WCF streaming service and came across couple of blogs that are very neat in explaining the basics. Take a look at those...

WCF Streaming: Upload files over HTTP
Transferring large files using WCF

Love coding!

Saturday, January 10, 2009

.Net Chart Control from Microsoft - Just when I needed

We might have used many chart controls over years and there are many good ones out there. But as just I was planning on upgrading one project to include advanced presentation parts, Microsoft comes up with free chart control! Check it out here and here.

Love coding!

Sunday, December 28, 2008

Windows Forms SetFocus to any control

Recently came across a developer need to have a utility that sets focus to a control anywhere on a windows form. Well, you might know about the Focus method of the controls but that might not navigate to the control in cases, say for example, when you have tab pages. So wrote this quick utility that takes the name of the control and a base control where to start the drill down (for most instances, you might want to give the base form here).

The code builds the list of controls recursively by finding the control and it's parents in parent-child hierarchy. Let's look at a simple code snippet for it:


namespace MyProject.ClientUtility
{
/// <summary>
/// Utility class for forms controls
/// </summary>
public class FormsUtility
{
private List<Control> _focusControlsList = new List<Control>();

public FormsUtility() { }

#region Set Focus to Control
/// <summary>
/// Set focus to a control based on the control name.
/// Pass base(parent) control to loop through all child controls.
/// </summary>
/// <param name="controlName"></param>
/// <param name="baseControl"></param>
/// <returns></returns>
public void SetFocusTo(string controlName, Control baseControl)
{
// Build the control list from parent to child order
BuildControlList(controlName, baseControl);
// Set focus in that order:
foreach (Control eachControl in _focusControlsList)
SetFocus(eachControl);
}

/// <summary>
/// Build the control list from Top-Down
/// </summary>
/// <param name="controlName"></param>
/// <param name="baseControl"></param>
/// <returns></returns>
private bool BuildControlList(string controlName, Control baseControl)
{
if (baseControl.Name == controlName)
{
_focusControlsList.Insert(0, baseControl);
return true;
}
foreach (Control eachControl in baseControl.Controls)
{
if (eachControl.Name == controlName)
{
_focusControlsList.Insert(0, eachControl);
return true;
}
if (eachControl.HasChildren)
{
// recursive...
if (BuildControlList(controlName, eachControl))
{
// going back to each parent to set focus
_focusControlsList.Insert(0, eachControl);
return true;
}
}
}
return false;
}

/// <summary>
/// Focus based on type of control
/// </summary>
/// <param name="controlToFocus"></param>
private void SetFocus(Control controlToFocus)
{
switch (controlToFocus.GetType().ToString())
{
case "System.Windows.Forms.TabPage":
// TabPage must be selected
((TabControl)((TabPage)controlToFocus).Parent).SelectedTab =
(TabPage)controlToFocus;
break;
default:
controlToFocus.Focus();
break;
}
}
#endregion
}
}

Love coding!

Wednesday, December 3, 2008

Toolbox Bitmap for Control - when icon not showing?

If you ever built any custom components, you might be familiar in creating toolbox bitmaps. To start with, take a look at this MSDN How To to get an idea of what I'm talking about. It's fairly simple and straightforward. However, I heard from quite few about having problems in getting this working. Most of them seem to have issues with assemblies and if you had made sure that's not the case then one more place I would suggest is going to the properties of the icon or bitmap itself in your project. Make sure that the Build Action is selected to as Embedded Resource before compiling your project.

Love coding!

Friday, November 7, 2008

How to track users when cookies are deleted? Super Cookies?

Ever wonder how some sites keep track of your visit even after clearing all the cookies and cache? Besides permanent cookies, which browsers can clear and also have an expiration, we can look into Flash based Local Shared Object model.

Take for instance Bank of America. According to their Privacy & Security statement they (also) use flash objects to remember your computer. Even when you delete your cookies, cache and temp files they can still recognize your user ID (when you opted to remember the computer) and bypass the security question. There are others that do similar techniques, like INGDirect and so on.

If you are wondering where the information is getting stored, then look under "C:\Documents and Settings\YourUSERID\Application Data\Macromedia\Flash Player\#SharedObjects". And to see how to set your preferences, check this article here.

While this is a neat technique to overcome ordinary user's cookie deletions, the technique is subject to concerns regarding how well it's being used to secure the data. Unlike cookies, flash cache is not something all the users are aware of. Hence before using this technique we need to ensure that the users are educated on risks if using a public machine and leaving a trail of flash cache.

Love coding!

Tuesday, October 28, 2008

Implement p3p headers - Cross-Site (Domain) iFrames

With ever growing popularity of widget based sites, I think, there is a trend of using iFrames in lot of web sites to show content/applications across domains. There are challenges though and one common one being that the cookies from the iFrame doesn't work when the parent is on a different domain.

If privacy is set to low then this might no be an issue; but since most users would have it set to medium the browser won't let the iFrame to write cookies if that's from a different domain than the parent. To resolve this problem, we need to let the browser allow the cookies from the iFrame site and this can be done with p3p headers.

To do this in a .Net application, we can use the Application_BeginRequest event to set the custom http header to allow p3p.


protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("p3p", "CP=\"CAO PSA OUR\"");
}


A rather easier way to do this in general, even for classic asp, is to set this in IIS. Go to site properties - HTTP Headers - Add Custom HTTP Header:
Name: p3p
Value: CP="CAO PSA OUR"




Love coding!

Friday, October 10, 2008

Trouble shooting? May be try 5 Whys

Trouble shooting - We all have seen it & done it (may be many many times). It may be a practice that some of us already use to try 5 Whys. It might be an engineering concept but certainly might be applied to any situation. There are shortcomings and criticisms to this approach, but its not a bad idea to try out.

Just recently saw a team struggling to get their application running properly. They had too many issues and saw them applying patches feverishly to make it work. I can understand the pressure under tough time lines and sympathize with the team. That's when I was thinking about the 5 Whys. As simple as it sounds, it is an effective way of getting to root cause of the problem.

On the lighter side, we have seen many times, teams get to the root cause and term it as bad initial design (so have to live with it...).

There is no one solution that fits all or solves all problems. Go by what we can do the best given the limitations but use the right tools (& force) to solve the problems we trouble shoot.

Love coding!

Wednesday, October 8, 2008

ASP.Net Data Cache & Oracle Database Change Notification

A good web application design has data caching built to it. It is always a fine balance to find out what’s the optimal level to keep the cache and the refresh intervals. In most of my designs I compartmentalize these into various types like 24 hour windows, specified number of hour windows, user types with specified number of hours… etc.


The question that follows naturally is how to force a cache refresh. Again, multiple ways to go about doing it and here I wanted to bring forward the concept with Oracle 10g called Database Change Notification methodology. Read this ODP.Net article from Oracle that goes in detail about this.


To highlight this concept in simple terms it is done with some sort of notification (typically an event) built in our applications. An example might look like this:


OracleConnection con = new OracleConnection("Your Connection String");

OracleCommand cmd = new OracleCommand("Your SQL", con);

con.Open();

// Register notification with command object if result changes.

// When an OracleDependency instance is bound to an OracleCommand

// instance, an OracleNotificationRequest is created and is set in the

// OracleCommand's Notification property. This indicates subsequent

// execution of command will register the notification.

OracleDependency dep = new OracleDependency(cmd);

// Allow the change notification handler in the database to persist

// even after the first database change

cmd.Notification.IsNotifiedOnce = false;

// Add the event handler to handle the notification.

// The OnDatabaseNotification method will be invoked when a notification

// message is sent from the database

dep.OnChange +=

new OnChangeEventHandler(OnDatabaseNotification);

OracleDataAdapter da = new OracleDataAdapter(cmd);

// Do your data stuff here...

// This event gets triggered on Database Change Notification

public static void OnDatabaseNotification(object src, OracleNotificationEventArgs args)

{

// Call your cache refresh method here...

}

Love coding!

Tuesday, October 7, 2008

Visual Studio - Web Project vs Web Site

Since Visual Studio 2005, I think, we see this question being asked quite frequently - what's the difference between a Web Proejct and a Web Site; and which one should I use. Well, I have used both and have my reasonings for using either. I agree with others that this is a decision left to individual scenario and situation; which the architect and/or the lead members can make a decision.

To help you to make an informed decision, here are some really nice articles that address this topic:

MSDN Article
Mike Hacker's Blog
Maor David's Blog
Damieng's Blog
Anthony Grace's Article

Love coding!

Wednesday, October 1, 2008

WCF Services - starters basic trouble shooting

I recommend starters to understand fundamental concepts of WCF from MSDN. I composed some of my answers to basic questions/trouble shooting items for beginners and posted on my knol - WCF: Beginners FAQ.

I will keep this list growing and if anybody has any suggestions/questions/additions I will be glad to include.

Love coding!

Thursday, September 25, 2008

Using javascript to enable/disable Asp.Net validators - plus a trick to force the validity

Let's take an example of payment options and required fields on an Asp.Net page: Say we have Credit card option and other payment option like PayPal or so. If CC is chosen, then we want to enforce the required validator on CC fields and otherwise not. And we want to do it in JavaScript - a simple way to go is to use the ValidatorEnable method -

ValidatorEnable(document.getElementById('<%=rqrdtxtCCNumber.UniqueID.ToString().Replace("$", "_")%>'), true);

Plus a trick I used recently with this method is to force the validity of a required field. To extend the same example above, if say we have JavaScript to copy address for CC from other fields when you check address is same check box and if we have required validators on the CC address fields, you may notice when you check (which copies) and uncheck (which clears) the our error indicator appears on those fields. To overcome, just call the same method like above in your copy or clear javascript - it works!

Love coding!

Wednesday, September 10, 2008

Membership Profile read updates LastUpdatedDate

This is in reference to custom membership provider we built for Oracle but the concept is similar to SQL Membership. Found that the profile table has both LastActivityDate and LastUpdatedDate getting updated. A little digging into it took me to the root cause of the problem - usage of Profile to read custom Profile properties (XML).


For example, any place we use Profile.LastName to get the last name then it's updating the LastActivityDate (as expected) and also updates LastUpdatedDate. Which is not intended. The idea is to update the latter column only when the user modifies and saves their profile.


If we need to use Profile.Get in any situation without updating the LastUpdatedDate then one of our choice is to use ProfileCommon. Here’s an idea on how to get that info in a class:


ProfileCommon myProfile = (ProfileCommon)ProfileCommon.Create(HttpContext.Current.User.Identity.Name);


If you are using this in a web page code then you can directly use GetProfile method, like:

ProfileCommon myProfile = Profile.GetProfile(User.Identity.Name);


And then to get your LastName property, just use myProfile.LastName.


Love coding!

Thursday, August 7, 2008

Windows Service InstallUtil - Service not showing up in services list

Another simple issue that took me a while to figure out what's happening. Well, have built many windows services and used both manual and install packages to deploy them. Recently, was using InstallUtil and the service didn't show up in the services list. The reason - I already had services window open. Closed it and reinstalled using installutil - there you go, it shows up.
Love coding!