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.

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

 

Common mistake in ASP.NET Forms Authentication

We are all used to this now…


    

    

    

    

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


  

        

             //Allow users in Customers role

         // Deny rest of all users

    

   

Common Mistake is to place 

 before 



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


  

        

             // Deny rest of all users

             //Allow users in Customers role       

    

   

VS 2010 SP1 (Beta) and IIS Express

ScottGu has blogged on the VS 2010 SP1 (Beta) and IIS Express, see his article here
Basically Visual Studio 2010 SP1 adds support for IIS Express
IIS Express is a free version of IIS 7.5 that is optimized for developer scenarios:

  • It’s lightweight and easy to install (less than 5Mb download and a quick install)
  • It does not require an administrator account to run/debug applications from Visual Studio
  • It enables a full web-server feature set – including SSL, URL Rewrite, and other IIS 7.x modules
  • It supports and enables the same extensibility model and web.config file settings that IIS 7.x support
  • It can be installed side-by-side with the full IIS web server as well as the ASP.NET Development Server (they do not conflict at all)
  • It works on Windows XP and higher operating systems – giving you a full IIS 7.x developer feature-set on all Windows OS platforms

Check out ScottGu’s blog and go for it.

ASP.NET Error Bar Chart

The Error Bar chart type consists of lines with markers that are used to display statistical information about the data displayed in a graph. A series of the Error Bar chart type has three Y values. While these values can be manually assigned to each point, in most cases, the values are calculated from the data present in another series. The order of the Y values is important because each position in the array of values represents a value on the error bar.


Adding Error Bar series to another series on same chart
Lets say we have a chart named Chart1 and series named Series1. 
We can add the Error Bar series to Series1 using the following snippet…
.

     Series errorBarSeries = new Series(“ErrorBar”);  

            errorBarSeries.ChartType = SeriesChartType.ErrorBar;  

            errorBarSeries.MarkerBorderColor = Color.FromArgb(64, 64, 64);  

            errorBarSeries.MarkerSize = 6;  

            errorBarSeries.YValuesPerPoint = 3;  

            errorBarSeries.BorderColor = Color.FromArgb(180, 26, 59, 105);  

            errorBarSeries.Color = Color.FromArgb(252, 180, 65);  

            errorBarSeries.ShadowOffset = 1;  

            errorBarSeries.MarkerStyle = MarkerStyle.None;  

            errorBarSeries[“PointWidth”] = “0.1”;  

            double error = 100;  

            foreach (DataPoint point in Series1.Points)  

            {  

                double centerY = point.YValues[0];  

                double lowerErrorY = centerY – error;  

                double upperErrorY = centerY + error;  

                errorBarSeries.Points.AddXY(point.XValue, centerY, lowerErrorY, upperErrorY);  

            }  

            Chart1.Series.Add(errorBarSeries); 

.
Calculating Error Values from Another Series
This can be done using the following code snippet.

  // Populate series with data
    double[]    yValues = {32.4, 56.9, 89.7, 98.5, 59.3, 33.8, 78.8, 44.6, 76.4, 68.9};
    Chart1.Series[“DataSeries”].Points.DataBindY(yValues);

    // Set error bar chart type
    chart1.Series[“ErrorBar”].ChartType = SeriesChartType.ErrorBar;

    // Link error bar series with data series
    Chart1.Series[“ErrorBar”][“ErrorBarSeries”] = “DataSeries”;

    // Set error calculation type
    Chart1.Series[“ErrorBar”][“ErrorBarType”] = “StandardError”;

    // Set error bar upper & lower error style
    Chart1.Series[“ErrorBar”][“ErrorBarStyle”] = “UpperError”;

    // Set error bar center marker style
    Chart1.Series[“ErrorBar”][“ErrorBarCenterMarkerStyle”] = “Circle”;
Adapted from:
MSDN Error Bar Chart     
Social MSDN Error Bar Chart

Server-side vs Client-side validation for ASP.NET web applications

What is validation?
In HTML ‘Validation’ is not a process, it is merely a concept. HTML simply offers client-side scripting and form-posting. With the web page disconnected, nothing will happen server side until the form is posted – after that nothing more can happen client side until a new web page (response) is received.
Client-side validation
Client-side scripting must occur before server-side scripting. ‘Validation’ then is a function of our code.
If our code uses client side scripting to validate, it must happen before the form post, if we use server side code to validate it must happen after the form post.
This obviously gives the option of using the client side scripting to cancel the form post if our validation routine is not satisfied.
In .Net Microsoft offered a wizard to generate validation scripts (the validation controls), but this merely generates code as explained above.
Client-side validation is fundamentally flawed in that the ‘client’ which, strictly speaking is outside the applications control, is detailing whether something is acceptable or not. So, for example, the user might have a browser that does not fully support JavaScript and invalid data might be returned as valid. For this reason it is always recommended to use server side validation.
Server-side validation
Server side validation on the other hand can be very frustrating and with unexpected results. A user can submit a form, wait for long time for it to be processed, only to be told that it is invalid. For this reason we add client side validation to check the form before it is submitted, merely to enhance userability.