Friday, June 03, 2011

Using C# String.Format “{0:p0}” without the leading space before percentage sign

Simplest expresssion is:  

String.Format("{0:0%}", 0.10)

A more elegant solution is:  

Use the NumberFormatInfo.PercentPositivePattern Property:
NumberFormatInfo numberInfo = new NumberFormatInfo();
numberInfo.PercentPositivePattern = 1;
Console.WriteLine(String.Format("{0}", 0.10.ToString("P0",numberInfo)));

Thursday, May 26, 2011

Formatting RDL/RDLC values as percentage

I thought I'd share how to format percentage values in ReportViewer report.
Say your data source has the following fields:
  • TotalLow
  • TotalHigh
  • Total

Suppose we want to display the values for TotalLow and TotalHigh as  percentage of Total.

We can use the following expressions.
=String.Format("{0:p0}",Sum(Fields!TotalLow.Value)/Sum(Fields!Total.Value))
=String.Format("{0:p0}",Sum(Fields!TotalHigh.Value)/Sum(Fields!Total.Value))

You notice the RDL/C allows you to use .NET String object Format method.

Hope this helps.

Thursday, May 12, 2011

SQL Service fails to start with error code 126

Got this tweet notification:
 
myitforum

Friday, May 06, 2011

Coding Standards, why the heck?

Coding standards. Yes. Coding standards. Why do we need them?

I was reviewing our companies coding style document and thought I share with you some references I have come across.

The essence of enforcing coding standards is to have coding style - which is good for management of your code base and consistency. And overall, improves productivity of your developers.

Here are some useful links on coding style:

Brad Abrams Internal coding style
MSDN: Design Guidelines for Developing Class Libraries
Google C++ Style Guide

Thursday, March 17, 2011

Calling ASP.NET WebMethod with more than one paramaters using JSON

Suppose we have a web method in ASP.NET web form or webservice

 [WebMethod]   
 public void GetItems(string itemName, string itemDesc)
 {

 }

 Using jQuery, we are going to call this method using the code like this below. I am pre-suming there is a search button which we have assigned id #searchNow
 

 $(document).ready(function () {  
  $('#searchNow').click(function () {
  $.ajax({
     type: "POST",
     url: "SearchItems.aspx/GetItems",
     data: "{'itemName':'book','itemDesc':'toys'}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
            success: function (msg) {
           
            }
        });
 });
  Our interest is how to pass parameters to the web method.
 In the snippet above the line


 data: "{'itemName':'book','itemDesc':'toys'}",

is used to pass parameters to the GetItems function. Please not that it must be a JSON compliant string

 

Friday, February 25, 2011

Constructing Javascript Date object using custom date string format e.g. dd/mm/yyyy

The Date object is used to work with dates and times. 
Date objects are created with the Date() constructor.
There are four ways of instantiating a date:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Date(dateString)
This Date constructor only accepts dateString in UTC format.
However, we can write our custom function that accepts a custom format, say dd/mm/yyyy, from which we can extract the date parts and use them to construct a Date object.

function parseDate(input, format) {
  format = format || 'dd/mm/yyyy'; // somedefault format
  var parts = input.match(/(\d+)/g),
      i = 0, fmt = {};
  // extract date-part indexes from the format
  format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });
  return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1,   parts[fmt['dd']]);
}
Usage
parseDate('01-31-2010', 'mm-dd-yyyy');
parseDate('31/01/2010', 'dd/mm/yyyy');
parseDate('2010/01/31');

Tuesday, February 22, 2011

Common mistake in ASP.NET Forms Authentication

We are all used to this now...


<configuration>

    <system.web>

    <authentication mode="Forms">

    </authentication>

    </system.web>

</configuration>


In ASP.NET Forms authentication, you can allow access to particular users or deny them using the allow and deny tags. Likewise, you can allow or deny access to particular roles.
E.g. to allow access to a page, say Customer, you will do


<location path="Customer">

  <system.web>

        <authorization>

            <allow roles="Customers"/> //Allow users in Customers role

        <deny users="*"/> // Deny rest of all users

    </authorization>

   </system.web>

</location>



Common Mistake is to place 

<deny../> before <allow ../>


This web config below will not allow users even if they are in Customers role


<location path="Customer">

  <system.web>

        <authorization>

            <deny users="*"/> // Deny rest of all users

            <allow roles="Customers"/> //Allow users in Customers role       

    </authorization>

   </system.web>

</location>