Log in
23
August

Organize Usings

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

During your development, you may notice that your code file contains using directives which are not required by your code. The Visual Studio IDE provides the Organize Usings option to remove/sort these using directives. To access this option, right-click anywhere within your code editor and select one of the sub-menu options for Organize Usings option.

  1. Remove Unused Usings : This option will remove any using directive not required by your code. Note : Please build your application before executing this option to ensure that any required directives are not removed.
  2. Sort Usings : This option will organize your Using directives alphabetically giving precedence to the System name-space.
  3. Remove and Sort : Use this option to execute the above operations together.

Organize Usings

15
July

Javascript Formatting

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

Like most developer I love jQuery and appreciate the intellisense support in Visual Studio. I am very particular about the format for my code and often use the Document Format option of the IDE. What I noticed was that my braces were never placed on new lines, and I wanted to change this. So here’s what you can do :

  1. In the Tools menu, select Options…
  2. Check the ShoAdd an Imagew all settings options
  3. In the Text Editor > JScript choose the Formatting option.
  4. On the right pane, there are two options under the ‘New lines’ section. Check them as per your requirement.

Javascript Formatting

19
June

I am a big fan of YouTube and how they revolutionized video sharing. One thing I didn’t like about the YouTube embedded player is that it does not have sufficient filtering depending on the content being played. I am referring to the Related Videos option that is available when you mouse over the video, or in some versions shown at the end of video playback. These related video suggestions, often have ‘mature’ content making the videos unsuitable for family content/blogs.

There is a very simple way of disabling the Related Video option, just append &rel=0 at the end of the url in the object embed code. According to the API Documentation at Google, setting ‘0′ to the rel parameter will also disable the search functionality.

For more information of the available parameter options, please visit http://code.google.com/apis/youtube/player_parameters.html

24
May

Quick Tip : SQL Server Reseed Identity Value

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

DBCC CHECKIDENT can reset the identity value of the table. The syntax is as follows :

DBCC CHECKIDENT (<table_name>, reseed, <seed_value>)

After executing this statement, the next inserted record will have seed_value + 1 as value. This rule is applicable only if the table previously contained records.

In case of a table with no records, since the current Seed Value is NULL, after execution of the above statement, the SEED VALUE is updated to 0. Thus when the first record is inserted, the SEED VALUE will be 0 not 1.

04
March

Here’s a small javascript helper to read Url Query String Parameters.

function GetUrlParams()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?')
					+ 1).split('&');

	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

The above function, will read the url, get the query string part i.e from (”?”) and split each paramter by the (”=”) character and create a name value pair allowing us to access the param value using the param name.

For example, if we are have a test.htm in http://www.example.com and the url is : http://www.example.com/test.htm?fname=Adnan&lname=Rashid

To access the param values we can use something like :


var params = GetUrlParams();

alert(params["fname"]);

alert(params["lname"]);
10
January

Multiline TextBox Length Validation

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

Unlike the normal TextBox, the MaxLength property doesnt work for MultiLine TextBox. Heres a small trick to solve the problem:

<asp:TextBox ID="txtExample" runat="server" TextMode="MultiLine" />

<asp:RegularExpressionValidator ID="revExample" runat="server"
ControlToValidate="txtExample" ValidationExpression="[\s\S]{1,200}"
Display="Dynamic" ErrorMessage="Length cannot be greater than 200 characters" />

The above markup validates the revExample TextBox / textarea and limits the max characters to 200. By modifying the regular expressions, we can validate fields for any scenario. If you are looking for some Regular Expressions to get started out, just point your browser to http://regexlib.com/

Cheers!

23
December

Conditional Breakpoints

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

Visual Studio is filled with nifty tricks, which if leveraged properly in the right situation can make the developers life easy. Consider if you had a looping statement, and wanted to debug it, but only for a particular value. Rather than hitting the breakpoint every time, you can shorten the hits by using a Conditional Breakpoint.

To set a Conditional Breakpoint, right-click the Breakpoint circle and select the Condition option.

Conditional Breakpoint

You can then define the condition and additionally select an option to filter the condition for evaluating the condition, or for raising the breakpoint when the value of the expression changes.

22
December

Attach to Process…

Written by Adnan Rashid. 2 comments Posted in: Development
Tagged with ,

The average programmer uses the general Debug mode by using the “Start Debugging” option from the Debug menu, or the F5 shortcut key. But what if you want to debug an application that is already running? If you faced this problem, then this tip should lighten up your day.

Choose Debug | Attach to Process….

Select the aspnet_wp.exe from the list of Available Processes and click on Attach

Attach To Process

Alternatively you can also use ALT + D , P to select the option.

22
November

jQuery Charts

Written by Adnan Rashid. No comments Posted in: Development
Tagged with ,

Although there are tons of charting / reporting plugins ranging from inline charts to flash charts, each one is unique in terms of technology, footprint or usage. I needed something compatible with jQuery and supported many as many chart types as possible. Here is the pick list:

1. jQuery Google Charting (http://www.keith-wood.name/gChart.html)

This plugin allows you to leverage the power and simplicity of Google Reporting. Easy to use with detailed examples, and wide support for chart types makes this an impressive must see.

2. Flot (http://code.google.com/p/flot/)

This plugin is very similar to its prototype counterpart Flotr. Again kudos for good examples and an excellent plugin.

3. Canvas Chart Plugin (http://www.filamentgroup.com/lab/creating_accessible_charts_using_canvas_and_jquery/)

Another great plugin, but does have a few rough edges on the cross-browser compatibility due to the issue of canvas tag support in some browsers like IE. Luckily there’s a good plugin to solve this problem, which they have also mentioned on their website.

18
November

vCalendar – C# Implementation

Written by Adnan Rashid. No comments Posted in: Development, General
Tagged with ,

I needed to integrate support for vCalendar in my current project. So i went through the vCalendar spec 1.0 at http://www.imc.org/pdi/vcal-10.txt and wrote this class. Although I have not implemented all the specification features, but this should prove sufficient for the usual requirements. If you have any comments / suggestions , let me know.

using System;
using System.Text;
using System.IO;
using System.Collections;

/// <summary>
/// Summary description for VCalendar
/// </summary>
public class VCalendar
{
	private string _productID;
	/// <summary>
	/// A unique identifier identifying the originating program
	/// </summary>
	public string ProductIdentifier
	{
		get { return _productID; }
		set { _productID = value; }
	}

	private ArrayList _categories;
	/// <summary>
	/// ArrayList of categories
	/// </summary>
	public ArrayList Categories
	{
		get { return _categories; }
		set { _categories = value; }
	}

	private DateTime _createdOn = DateTime.MinValue;
	/// <summary>
	/// DateTime that the vEvent was created.
	/// </summary>
	public DateTime CreatedOn
	{
		get { return _createdOn; }
		set { _createdOn = value; }
	}

	private DateTime _completedOn = DateTime.MinValue;
	/// <summary>
	/// DateTime the vEvent completed on
	/// </summary>
	public DateTime CompletedOn
	{
		get { return _completedOn; }
		set { _completedOn = value; }
	}

	private DateTime _startOn = DateTime.MinValue;
	/// <summary>
	/// DateTime that the vEvent will start
	/// </summary>
	public DateTime StartOn
	{
		get { return _startOn; }
		set { _startOn = value; }
	}

	private DateTime _endOn = DateTime.MinValue;
	/// <summary>
	/// DateTime that the vEvent will end
	/// </summary>
	public DateTime EndOn
	{
		get { return _endOn; }
		set { _endOn = value; }
	}

	private ClassificationType _classification = ClassificationType.PUBLIC;
	/// <summary>
	/// Defines the access classification for the vEvent entity
	/// </summary>
	public ClassificationType Classification
	{
		get { return _classification; }
		set { _classification = value; }
	}

	private int _priority = 0;
	/// <summary>
	/// 0 = undefined, 1 = High, -1 = Low. Defaults to 0
	/// </summary>
	public int Priority
	{
		get { return _priority; }
		set { _priority = value; }
	}

	private string _subject;
	/// <summary>
	/// Subject of the vEvent
	/// </summary>
	public string Subject
	{
		get { return _subject; }
		set { _subject = value; }
	}

	private string _description;
	/// <summary>
	/// Description of the vEvent
	/// </summary>
	public string Description
	{
		get { return _description; }
		set { _description = value; }
	}

	private bool _enableAlarm;
	/// <summary>
	/// Flag to enable the reminder alarm
	/// </summary>
	public bool EnableAlarm
	{
		get { return _enableAlarm; }
		set { _enableAlarm = value; }
	}

	public override string ToString()
	{
		StringBuilder sb = new StringBuilder();
		sb.Append(“BEGIN:VCALENDAR\n”);
		sb.Append(“VERSION:1.0\n”);
		sb.Append(“BEGIN:VEVENT\n”);

		// Product Identifier
		if (!string.IsNullOrEmpty(_productID))
			sb.AppendFormat(“PRODID:{0}\n”, _productID);

		// Categories
		if (_categories != null && _categories.Count != 0)
		{
			sb.Append(“CATEGORIES:”);
			for (int i = 0; i < _categories.Count; i++)
			{
				if (i == 0)
					sb.Append(_categories[0].ToString());
				else
					sb.AppendFormat(“;{0}”, _categories[i].ToString());
			}
			sb.Append(“\n”);
		}

		// Created On
		if (_createdOn != DateTime.MinValue)
			sb.AppendFormat(“DCREATED:{0}\n”, _createdOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Start Date Time
		if (_startOn != DateTime.MinValue)
		{
			sb.AppendFormat(“DTSTART:{0}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

			// Display Reminder
			if (_enableAlarm)
				sb.AppendFormat(“DALARM:{0};PT5M;2;{1}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”), _subject);
		}

		// End Date Time
		if (_endOn != DateTime.MinValue)
			sb.AppendFormat(“DTEND:{0}\n”, _endOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Completed On
		if (_completedOn != DateTime.MinValue)
			sb.AppendFormat(“COMPLETED:{0}\n”, _completedOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Priority
		if (_priority != 0)
			sb.AppendFormat(“PRIORITY:{0}\n”, _priority);

		// Description
		if (!string.IsNullOrEmpty(_description))
			sb.AppendFormat(“DESCRIPTION:{0}\n”, _description);

		// Subject
		if (!string.IsNullOrEmpty(_subject))
			sb.AppendFormat(“SUMMARY:{0}\n”, _subject);
		if (_classification != ClassificationType.PUBLIC)
			sb.AppendFormat(“CLASS:{0}\n”, Enum.GetName(typeof(ClassificationType), Classification));
		sb.Append(“END:VEVENT\n”);
		sb.Append(“END:vCalendar”);
		return sb.ToString();
	}

	public void Generate(string filePath, FileMode mode)
	{
		FileStream fs = new FileStream(filePath, mode);
		StreamWriter sw = new StreamWriter(fs);
		using (sw)
		{
			sw.Write(this.ToString());
		}
	}
}

public class Attendee
{
	private StatusType _status;
	public StatusType Status
	{
		get { return _status; }
		set { _status = value; }
	}

	private AttendeeType _role;
	public AttendeeType Role
	{
		get { return _role; }
		set { _role = value; }
	}

	public string _attendeeName;
	public string AttendeeName
	{
		get { return _attendeeName; }
		set { _attendeeName = value; }
	}

	public override string ToString()
	{
		StringBuilder sb = new StringBuilder();
		if (!string.IsNullOrEmpty(_attendeeName))
		{
			sb.Append(“ATTENDEE”);
			if (_role != AttendeeType.ATTENDEE)
				sb.AppendFormat(“;ROLE={0}”, Enum.GetName(typeof(AttendeeType), Role));
			if (_status != StatusType.NEEDS_ACTION)
				sb.AppendFormat(“;STATUS={0}”, Enum.GetName(typeof(StatusType), Status));
			sb.AppendFormat(“:{0}\n”, _attendeeName);
			return sb.ToString();
		}
		else
			return string.Empty;
	}
}
public enum AttendeeType
{
	ATTENDEE,
	ORGANIZER,
	OWNER,
	DELEGATE,
}
public enum StatusType
{
	ACCEPTED,
	NEEDS_ACTION,
	SENT,
	TENTATIVE,
	CONFIRMED,
	DECLINED,
	COMPLETED,
	DELEGATED
}
public enum RSVPType
{
	YES,
	NO
}
public enum ExpectType
{
	FYI,
	REQUIRE,
	REQUEST,
	IMMEDIATE
}
public enum CategoryType
{
	APPOINTMENT,
	BUSINESS,
	EDUCATION,
	HOLIDAY,
	MEETING,
	MISCELLANEOUS,
	PERSONAL,
	TRAVEL,
	VACATION
}
public enum ClassificationType
{
	PUBLIC,
	PRIVATE,
	CONFIDENTIAL
}