Adnan’s Blog

Posts Tagged ‘tips & tricks

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


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[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 and the url is :

To access the param values we can use something like :

var params = GetUrlParams();



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


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 BreakpointYou 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.

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...

Attach To Process...

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

Heres a quick tip. If you need to include a request for read receipt like Microsoft Outlook while sending mails through, just add the Disposition-Notification-To Header to the MailMessage.

The MailMessage class does not have a corresponding attribute and thus it has to be set through the Header.

MailMessage mm = new MailMessage(fromAddress, toAddress);
mm.Subject = subject;
mm.Headers.Add("Disposition-Notification-To", sendReadReceiptToAddress);

Please keep in mind that this is only a request, and can be rejected/ignored by the Mail Client/User configuration.

In my current project I needed to access the Session state in a Generic Handler. To my dismay, i realized that the Session StateBag was null. The StateBag class is sealed and manages the Viewstate of the server controls and pages.

Upon discussion with my colleague, Mr. Hamed, we found the solution to my problem. Using the IRequiresSessionState interface in System.Web.SessionState, we got access to Session state.

According to Microsoft “Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.

Another option was to use the IReadOnlySessionState interface (Specifies that the target HTTP handler requires only read access to session-state values. This is a marker interface and has no methods.)

Needless to say, there is a performance increase in the latter case and if your requirement is only to read Session state, then that would be the right choice

Top Posts

Blog Stats

  • 4,271 hits

Top Clicks

  • None


View Adnan Rashid's profile on LinkedIn

Adnan Rashid