Friday, March 6, 2009

Convert Number to words (for Indian rupees and other currency ) in C# Asp.Net

public class NumberToWordsForCurrency
{


#region "--Methods--"
public string ConvertToInteranationWords(string numb)
{
string RetVal = string.Empty;
numb = numb.Trim();
if (this.IsNumber(numb))
{
try
{
string currency = "$ ";
string _currency = " cents Only.";
string Number;
string deciml;
string _number;
string _deciml;

this.Initialize();
string[] no = numb.Split('.');
if (no.Length == 1)
{
if (no[0].Length >= 1 && Convert.ToDouble(no[0]) > 0)
{
Number = no[0];
_number = (NameOfNumber(Number));
RetVal = currency + _number + " Only.";
}
else
{
RetVal = currency + " Zero Only.";
}

}
else if (no.Length == 2)
{
//int number part
if (no[0].Length >= 1 && Convert.ToDouble(no[0]) > 0)
{
Number = no[0];
_number = (NameOfNumber(Number));
}
else
{
_number = "Zero";
}
//decimal number part
if (no[1].Length >= 1 && Convert.ToDouble(no[1]) > 0)
{
deciml = no[1];
if (no[1].Length == 1)
{
deciml = Convert.ToString(Convert.ToDouble(no[1]) * 10);
}
_deciml = (NameOfNumber(deciml));
}
else
{
_deciml = "Zero";
}
// when both int and decimal part conbined.
RetVal = currency + _number.Trim() + " and " + _deciml.Trim() + _currency;
}

}
catch (Exception ex)
{

}
}
else
{
RetVal = "Invalid Value";
}

return RetVal;
}
#region "--Private variables--"
string[] US = new string[1003];
string[] SNu = new string[20];
string[] SNt = new string[10];

#endregion

///
///
///

private void Initialize()
{
SNu[0] = "";
SNu[1] = "One";
SNu[2] = "Two";
SNu[3] = "Three";
SNu[4] = "Four";
SNu[5] = "Five";
SNu[6] = "Six";
SNu[7] = "Seven";
SNu[8] = "Eight";
SNu[9] = "Nine";
SNu[10] = "Ten";
SNu[11] = "Eleven";
SNu[12] = "Twelve";
SNu[13] = "Thirteen";
SNu[14] = "Fourteen";
SNu[15] = "Fifteen";
SNu[16] = "Sixteen";
SNu[17] = "Seventeen";
SNu[18] = "Eighteen";
SNu[19] = "Nineteen";
SNt[2] = "Twenty";
SNt[3] = "Thirty";
SNt[4] = "Forty";
SNt[5] = "Fifty";
SNt[6] = "Sixty";
SNt[7] = "Seventy";
SNt[8] = "Eighty";
SNt[9] = "Ninety";

US[1] = "";
US[2] = "Thousand";
US[3] = "Million";
US[4] = "Billion";
US[5] = "Trillion";
US[6] = "Quadrillion";
US[7] = "Quintillion";
US[8] = "Sextillion";
US[9] = "Septillion";
US[10] = "Octillion";


}
///
///
///

///
///
public string NameOfNumber(string Number)
{
string GroupName = "";
string OutPut = "";
if ((Number.Length % 3) != 0)
{
Number = Number.PadLeft((Number.Length + (3 - (Number.Length % 3))), '0');
}
string[] Array = new string[Number.Length / 3];
Int16 Element = -1;
Int32 DisplayCount = -1;
bool LimitGroupsShowAll = false;
int LimitGroups = 0;
bool GroupToWords = true;
for (Int16 Count = 0; Count <= Number.Length - 3; Count += 3)
{
Element += 1;
Array[Element] = Number.Substring(Count, 3);
}
if (LimitGroups == 0)
{
LimitGroupsShowAll = true;
}
for (Int16 Count = 0; (Count <= ((Number.Length / 3) - 1)); Count++)
{
DisplayCount++;
if (((DisplayCount < LimitGroups) || LimitGroupsShowAll))
{
if (Array[Count] == "000") continue;
{
GroupName = US[((Number.Length / 3) - 1) - Count + 1];
}
if ((GroupToWords == true))
{
OutPut += Group(Array[Count]).TrimEnd(' ') + " " + GroupName + " ";
}
else
{
OutPut += Array[Count].TrimStart('0') + " " + GroupName;
}
}
}
Array = null;
return OutPut;
}
///
///
///

///
///
private string Group(string Argument)
{
string Hyphen = ""; string OutPut = ""; Int16 d1 = Convert.ToInt16(Argument.Substring(0, 1));
Int16 d2 = Convert.ToInt16(Argument.Substring(1, 1));
Int16 d3 = Convert.ToInt16(Argument.Substring(2, 1));
if ((d1 >= 1))
{
OutPut += SNu[d1] + " hundred ";
}
if ((double.Parse(Argument.Substring(1, 2)) < 20))
{
OutPut += SNu[Convert.ToInt16(Argument.Substring(1, 2))];
}
if ((double.Parse(Argument.Substring(1, 2)) >= 20))
{
if (Convert.ToInt16(Argument.Substring(2, 1)) == 0)
{
Hyphen += " ";
}
else
{
Hyphen += " ";
}
OutPut += SNt[d2] + Hyphen + SNu[d3];
}
return OutPut;
}
///
/// Test whether string is a number or not.
///

///
/// True if its a valid number.
private Boolean IsNumber(string num)
{
Boolean RetVal = false;
num = num.Trim();
if (num.Length > 0)
{
try
{
double test;
test = Convert.ToDouble(num);
RetVal = true;
}
catch (System.FormatException ex)
{
//throw;
}
catch (Exception ex)
{
//throw;
}
}
return RetVal;
}


public string ConvertToINR(string numb)
{
string RetVal = string.Empty;
numb = numb.Trim();
if (this.IsNumber(numb))
{
try
{
string currency = "Rupees ";
string _currency = " Paise Only.";
string Number;
string deciml;
string _number;
string _deciml;

this.Initialize();
string[] no = numb.Split('.');
if (no.Length == 1)
{
if (no[0].Length >= 1 && Convert.ToDouble(no[0]) > 0)
{
Number = no[0];
_number = (NameOfINRNumber(Number));
RetVal = currency + _number + " Only.";
}
else
{
RetVal = currency + " Zero Only.";
}

}
else if (no.Length == 2)
{
//int number part
if (no[0].Length >= 1 && Convert.ToDouble(no[0]) > 0)
{
Number = no[0];
_number = (NameOfINRNumber(Number));
}
else
{
_number = "Zero";
}
//decimal number part
if (no[1].Length >= 1 && Convert.ToDouble(no[1]) > 0)
{
deciml = no[1];
if (deciml.Length > 2)
{
deciml = deciml.Substring(0, 2);
}
if (no[1].Length == 1)
{
deciml = Convert.ToString(Convert.ToDouble(no[1]) * 10);
}
_deciml = (NameOfINRNumber(deciml));
}
else
{
_deciml = "Zero";
}
// when both int and decimal part conbined.
RetVal = currency + _number.Trim() + " and " + _deciml.Trim() + _currency;
}

}
catch (Exception ex)
{

}
}
else //not a number
{
RetVal = "Invalid Input.";
}
return RetVal;

}

private string NameOfINRNumber(string Number)
{
string RetVal = string.Empty;
try
{
int len = Number.Length;
if (len >= 0 && len <= 5)
{
RetVal = (NameOfNumber(Number));
}
else if (len > 5 && len <= 7)
{
RetVal = NameOfNumber(Number.Substring(0, len - 5)) + " Lakh " + NameOfNumber(Number.Substring(len - 5));
}
else if (len > 7 && len <= 9)
{
RetVal = NameOfNumber(Number.Substring(0, len - 7)) + " Crore " + NameOfNumber(Number.Substring(len - 7, 2)) + " Lakh " + NameOfNumber(Number.Substring(len - 5));
}
else if (len > 9 && len <= 11)
{
RetVal = NameOfNumber(Number.Substring(0, len - 9)) + " Arab " + NameOfNumber(Number.Substring(len - 9, 2)) + " Crore " + NameOfNumber(Number.Substring(len - 7, 2)) + " Lakh " + NameOfNumber(Number.Substring(len - 5));
}
else if (len > 11 && len <= 13)
{
RetVal = NameOfNumber(Number.Substring(0, len - 11)) + " Kharab " + NameOfNumber(Number.Substring(len - 11, 2)) + " Arab " + NameOfNumber(Number.Substring(len - 9, 2)) + " Crore " + NameOfNumber(Number.Substring(len - 7, 2)) + " Lakh " + NameOfNumber(Number.Substring(len - 5));
}
else if (len > 13)
{
RetVal = NameOfNumber(Number);
}

}
catch (Exception ex)
{
//throw new Exception("The method or operation is not implemented.");
}
return RetVal;
}

#endregion






}

Wednesday, February 25, 2009

Creating a XML document in .Net

Creating a XML document with C#
using System;
using System.Xml;

namespace AlokKumarSinha
{
class MainClass
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
XmlElement xmlelem2;
XmlText xmltext;
static void Main(string[] args)
{
MainClass app=new MainClass();
}
public MainClass() //constructor
{
xmldoc=new XmlDocument();
//let's add the XML declaration section
xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
xmldoc.AppendChild(xmlnode);
//let's add the root element
xmlelem=xmldoc.CreateElement("","root","");
xmltext=xmldoc.CreateTextNode("root text");
xmlelem.AppendChild(xmltext);
xmldoc.AppendChild(xmlelem);
//let's add another element (child of the root)
xmlelem2=xmldoc.CreateElement("","child1","");
xmltext=xmldoc.CreateTextNode("The text of the child1 element");
xmlelem2.AppendChild(xmltext);
xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);
//let's try to save the XML document in a file: C:\alok.xml
try
{
xmldoc.Save("c:\alok.xml"); //I've chosen the c:\ for the resulting file alok.xml
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}

Creating a XML document with VB.Net

Class MainClass
Private xmldoc As XmlDocument
Private xmlnode As XmlNode
Private xmlelem As XmlElement
Private xmlelem2 As XmlElement
Private xmltext As XmlText
Private Shared Sub Main(ByVal args As String())
Dim app As New MainClass()
End Sub
Public Sub New()
'constructor
xmldoc = New XmlDocument()
'let's add the XML declaration section
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "")
xmldoc.AppendChild(xmlnode)
'let's add the root element
xmlelem = xmldoc.CreateElement("", "root", "")
xmltext = xmldoc.CreateTextNode("root text")
xmlelem.AppendChild(xmltext)
xmldoc.AppendChild(xmlelem)
'let's add another element (child of the root)
xmlelem2 = xmldoc.CreateElement("", "Child1", "")
xmltext = xmldoc.CreateTextNode("The text of child1")
xmlelem2.AppendChild(xmltext)
xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2)
'let's try to save the XML document in a file: C:\alok.xml
Try
xmldoc.Save("c:" & vbNullChar & "alok.xml")
'I've chosen the c:\ for the resulting file alok.xml
Catch e As Exception
Console.WriteLine(e.Message)
End Try
Console.ReadLine()
End Sub
End Class

Saturday, February 7, 2009

To add a search engine to web page

To add a search engine to my web page that my users be able to search content of my website(all the pages not only the first page) ::
Solution
1. Visit This link:: http://forums.asp.net/p/1217595/2163891.aspx.

2.To have internal ASP.NET Search engine for your pages, have a look here: Internal Site Search Engine
To have google search engine on your websitem, follow this link: http://www.google.com/coop/cse/

Monday, February 2, 2009

Code for Downloading and Uploading Files in Asp.net

Code for downloading files ::
Sub Page_Load(Sender As Object, E As EventArgs)
Dim strRequest As String = Request.QueryString("file") '-- if something was passed to the file querystring If strRequest <> "" Then 'get absolute path of the file Dim path As String = Server.MapPath(strRequest) 'get file object as FileInfo Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server If file.Exists Then 'set appropriate headers Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) Response.AddHeader("Content-Length", file.Length.ToString()) Response.ContentType = "application/octet-stream" Response.WriteFile(file.FullName) Response.End 'if file does not exist Else Response.Write("This file does not exist.") End If 'nothing in the URL as HTTP GET Else Response.Write("Please provide a file to download.")
End If
End Sub
---------
Code for uploading the Files::
in markup side::-
---
asp:FileUpLoad id="FileUpLoad1" AlternateText="You cannot upload files" runat="server"
asp:Button id="Button1" Text="Upload" OnClick="Button1_Click" runat="server"
asp:Label id="Label1" runat="server"
---------------------in CodeBehind::
Sub Button1_Click(sender As Object, e As EventArgs) if FileUpLoad1.HasFile 'Uncomment this line to Save the uploaded file 'FileUpLoad1.SaveAs("C:\SomePhysicalPath" & FileUpLoad1.Filename) Label1.Text = "Received " & FileUpLoad1.FileName & " Content Type " & FileUpLoad1.PostedFile.ContentType & " Length " & FileUpLoad1.PostedFile.ContentLength else Label1.Text = "No uploaded file" end if end sub

Wednesday, January 28, 2009

problem inserting a date into a SQL server

You should format the date using the ISO unseparated date format, which is YYYYMMDD. Do not use any separator for the year, month, or day. Like this....

Insert Into SomeTable(DateColumn) Values('20090128')
Or
Update SomeTable Set DateColumn = '20090128' Where Id = 1

If you have a time included with your date, then the format should be: YYYYMMDD hh:mm:ss
Ex: '20090128 5:05 PM'
If you use this format, you will never have a problem.

Saturday, January 24, 2009

On Refresh of Page avoid adding of new data in database

use:

Response.Redirect("~/samepage",false);

Navigating Pages in asp.net

If you are trying to go to another page:
Response.Redirect ("Page.aspx")

If you are trying to go to a folder:
Response.Redirect ("~/Folder/Page.aspx")

If you are trying to come from a folder:
Response.Redirect ("../Page.aspx")