.NET Technical bits

Friday, June 15, 2012

Convert number to words using C#

Convert number to words using C#

static string[] ones ={"", "one" , "two", "three", "four", "five", "six", "seven", "eight" , "nine", "ten" ,
"eleven", "twelve" , "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};




static string[] tens ={"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};



static string[] thou = { "", "thousand", "lakh"};

public static string NumberToWords(int inputNum)

{

int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;

string retval = "";

string x = "";

try

{

bool isNegative = false;

if (inputNum < 0)

{

isNegative = true;

inputNum *= -1;

}



if (inputNum == 0)

return ("ZERO");



string s = inputNum.ToString();



while (s.Length > 0)

{

// Get the three rightmost characters

x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);



// Separate the three digits

threeDigits = int.Parse(x);

lasttwo = threeDigits % 100;

dig1 = threeDigits / 100;

dig2 = lasttwo / 10;

dig3 = (threeDigits % 10);



// append a "thousand" where appropriate

if (level > 0 && dig1 + dig2 + dig3 > 0)

{

retval = thou[level] + " " + retval;

retval = retval.Trim();

}



// check that the last two digits is not a zero

if (lasttwo > 0)

{

if (lasttwo < 20) // if less than 20, use "ones" only

retval = ones[lasttwo] + " " + retval;

else // otherwise, use both "tens" and "ones" array

retval = tens[dig2] + " " + ones[dig3] + " " + retval;

}



//Lakhs

if (level > 0 && dig1 > 0)

{

if (dig1 > 1)

{

retval = ones[dig1] + " lakhs " + retval;

}

else

{

retval = ones[dig1] + " lakh " + retval;

}

}// if a hundreds part is there, translate it

else if (dig1 > 0)

retval = ones[dig1] + " hundred " + retval;



s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";

level++;

}



while (retval.IndexOf(" ") > 0)

retval = retval.Replace(" ", " ");



retval = retval.Trim();



if (isNegative)

retval = "negative " + retval;

}

catch (Exception)

{

}

return (retval);

}

Wednesday, May 11, 2011

Difference between WCF and ASP.NET Web Services

Data translation:
ASP.NET web service depends on defining data and relies on the XmlSerializer to transform data to or from a service.

There are few issues with XmlSerializer to XML from serialize .NET types.

• The translated XML will have only the public fields or properties of .NET types

• The implemented class from IDictionary (ex: hash table) cannot be translated

• This will have the classes which implement IEnumerable interface

The WCF uses the attributes DataContract attribute and DataMemeber attribute to translate .NET types in to XML.

[DataContract]
public class Member
{
       [DataMember]
       public int memberID;
       [DataMember]
       public string memberName;
       [DataMember]
       public decimal salary;
}

The DataContractAttribute can be used for class or a struct. DataMemberAttribute can be used for private/public field or property.

DataContractSerializer and XMLSerializer:

• Performance wise DataContractSerializer better than XMLserialization

• DataContratSerializer Explicitly shows the which fields or properties are serialized into XML but XMLSerializer is not

• Hash Table can be translated in DataContractSerializer

Creating WCF and Web Services:

When we create a web service the WebService attribute need to be added to the web service class and WebMethod Attribute need to be added for any web service class methods.
For Example:

[WebService]
public class MyService : System.Web.Services.WebService
{
      [WebMethod]
      public string InvokeMe(string message)
     {
            return message;
     }
}

To create a WCF service we will write this code:
[ServiceContract]
public interface IMyService
{
        [OperationContract]
        string InvokeMe(string message);
}

public class MyService : IMyService
{
        public string InvokeMe (string message)
       {
              return message;
       }
}

The interface defines a WCF service contract using the ServiceContract attribute and the OperationContract tells which of the methods of the interface defines the operations of the service contract. The service type in WCF will be referred to as a class that implements the service.

Hosting the Services:
ASP.NET web services are created and compiled and will be hosted in IIS. This hosted will have the service file with extension .asmx and will have assembly with the compiled class. The hosted .asmx url can be added as a web reference to a .NET project so that the class and methods from service will be accessible in the project.
WCF Service can be hosted within IIS or WindowsActivationService using the steps below.

• Compile the service type into a class library.

• Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory.

• Copy the web.config file into the virtual directory.

Tuesday, April 26, 2011

Asp.NET Chart - How to specify Data

The chart area(s), series, and data points can be specified declaratively (either by entering the declarative mark-up by hand or via the Properties window) or programmatically in the code-behind class. Typically the series and chart area(s) are specified declaratively and the data points are populated programmatically by querying a database or some other dynamic data store. Let's start by discussing how to display a chart whose chart area, series, and data points are all specified declaratively.


<asp:Chart ID="chtNBAChampionships" runat="server">
<Series>

<asp:Series Name="Championships" YValueType="Int32" ChartType="Column" ChartArea="MainChartArea">

<Points>

<asp:DataPoint AxisLabel="Celtics" YValues="17" />

<asp:DataPoint AxisLabel="Lakers" YValues="15" />

<asp:DataPoint AxisLabel="Bulls" YValues="6" />

<asp:DataPoint AxisLabel="Spurs" YValues="4" />

<asp:DataPoint AxisLabel="76ers" YValues="3" />

<asp:DataPoint AxisLabel="Pistons" YValues="3" />

<asp:DataPoint AxisLabel="Warriors" YValues="3" />

</Points>

</asp:Series>

</Series>

<ChartAreas>

<asp:ChartArea Name="MainChartArea">

</asp:ChartArea>

</ChartAreas>

</asp:Chart>

Note that the Chart control has a <Series> section and a <ChartAreas> section, which define the series and the chart areas, respectively. The <ChartAreas> section in the above mark-up defines a single ChartArea named MainChartArea. The <Series> section defines a single Series named Championships. This Championship series is configured to render as a column and is displayed in the MainChartArea. Next, its data points are defined via the <Points> collection. There are seven data points, each data point displaying the NBA team name via the AxisLabel property and the number of championships in the YValues property.

After defining this mark-up, visit this page through a browser. You should see a chart in your browser similar to the screen shot below: