Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

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)));

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

 

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>

Wednesday, December 22, 2010

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

Friday, November 12, 2010

Visual Studio 2010 Productivity Power Tools

You can check out the latest VS2010 Productivity Power tools on the visualstudiogallery portal.


I love the Solution Navigator, Searchable and Reference Dialog amongst the other gallery of extensions!

Monday, November 01, 2010

Why I’m a better software developer than you

I came across this article and thought it was good enough to share with you.


What makes one developer better than another? Shouldn’t we all be performing at the same level?...Read more here

Friday, August 20, 2010

Robert The Grey: I don’t want your stinkin’ code comments

Robert The Grey: I don’t want your stinkin’ code comments: "I just read a fantastic thought experiment being put forward and trialled by Jesse Liberty at the moment. I’ve written this post in support ..."

Tuesday, April 27, 2010

Creating custom .NET Exceptions in C#

public class XyzlException : Exception
{
public XyzlException (string errorMessage, Exception inner) : base(errorMessage, inner)
{}
}


You can pass in a custom error message to your new Exception type.

Just try it out...

Monday, March 20, 2006

Printer Trays Hell, View Pro and Vb 6

Hi Everyone,

I am a Windows Applications developer.

Have you come across hell when trying to specify printer's Paper Source Trays from Vb6 application? I have actually been to hell!

Problem:
VB 6
exposes a Printer object, which points to the currently active printer for the current user profile in Windows. You can manipulate this object directly and set the printer trays with easy. However, if you use a third party tool called ViewPro you may experience problems.


ViewPro hides the printer object and at times it passes the wrong values for the printer trays.

Cause
ViewPro exposes a property called 'DefaultSource' which is an Enum, whilst the printer tray Ids may not lie in the Enum definations


Result
Print jobs may not always print to the correct trays. You have to find ways of circumventing the ViewPro and print directly to the printer.