public static string NumbersToWords(int inputNumber)
{
int inputNo = inputNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (inputNo < 0)
{
sb.Append("Minus ");
inputNo = -inputNo;
}
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i == 0)
sb.Append("and ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}

Programming is fun and interesting. As you all know, sometime we programmers stucks in the middle way of coding without getting a solution. Blogs like this will help us to find a path to solve our problems. The contents of this blog are the solutions that I found by myself and from other sources. Sharing such solutions is also interesting. Thank you!
Wednesday, September 3, 2014
C# code to convert Number to Words
Tuesday, August 26, 2014
Pivot table in SQL Server 2008 - Example
Introduction
Sample Table
A pivot table is a regularly used technique of shortening and displaying particular report information by means of consortium and aggregating values. Pivot tables are easily shaped by office users using Microsoft Excel or MS Access. Sincepivot table enables information builders and BI (Business Intelligence) specialists authorize their appearance of information and amplify the visibility andunderstandabilityof mined information, pivot tables are widespread and favored widely. Pivot tables exhibit information in tabular form. The pivot table formatting is not dissimilar than a tabular report formatting. But the tuples are produced by the statement information itself.
In this example i am using a temp table.
Sample Table
CREATE TABLE #Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT) INSERT INTO #Product(Cust, Product, QTY) VALUES('KATE','VEG',2) INSERT INTO #Product(Cust, Product, QTY) VALUES('KATE','SODA',6) INSERT INTO #Product(Cust, Product, QTY) VALUES('KATE','MILK',1) INSERT INTO #Product(Cust, Product, QTY) VALUES('KATE','BEER',12) INSERT INTO #Product(Cust, Product, QTY) VALUES('FRED','MILK',3) INSERT INTO #Product(Cust, Product, QTY) VALUES('FRED','BEER',24) INSERT INTO #Product(Cust, Product, QTY) VALUES('KATE','VEG',3) Select * from #Product order by Cust
Cust Product QTY
------------------------- -------------------- -----------
FRED MILK 3
FRED BEER 24
KATE VEG 3
KATE VEG 2
KATE SODA 6
KATE MILK 1
KATE BEER 12
(7 row(s) affected)
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS FROM ( SELECT CUST, PRODUCT, QTY FROM #Product) up PIVOT ( SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS) ) AS pvt ORDER BY CUST
CUST VEG SODA MILK BEER CHIPS --------- ----------- ----------- ----------- ----------- ----------- --------- FRED NULL NULL 3 24 NULL KATE 5 6 1 12 NULL (2 row(s) affected)
Pivot table based on product field
SELECT PRODUCT, FRED, KATE FROM ( SELECT CUST, PRODUCT, QTY FROM #Product) up PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt ORDER BY PRODUCT
PRODUCT FRED KATE
-------------------- ----------- -----------
BEER 24 12
MILK 3 1
SODA NULL 6
VEG NULL 5
(4 row(s) affected)
Friday, August 22, 2014
Checking Image resolution using Javascript - Asp.net File Uploader
Script
Download jquery.min.2.0.2.js
<script src="js/jquery.min2.0.2.js" type="text/javascript"></script>
<script type="text/javascript">
function setEvent() {
$(document.getElementById('<%=flpPhoto.ClientID%>')).change(function() {
// alert(this.files[0]);
var reader = new FileReader();
var image = new Image();
reader.readAsDataURL(this.files[0]);
reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function() {
var w = this.width;
var h = this.height;
// alert(w + ' ' + h);
if (w != 155 && h != 200) {
$(document.getElementById('<%=flpPhoto.ClientID%>')).val('');
alert('Invalid Student Photo Size. Please upload photo of resolution 155x200 Pixels.');
}
};
};
});
}
function pageLoad() {
setEvent();
}
</script>
HTML
<asp:FileUpload ID="flpPhoto" TabIndex="52" runat="server" />
Download jquery.min.2.0.2.js
<script src="js/jquery.min2.0.2.js" type="text/javascript"></script>
<script type="text/javascript">
function setEvent() {
$(document.getElementById('<%=flpPhoto.ClientID%>')).change(function() {
// alert(this.files[0]);
var reader = new FileReader();
var image = new Image();
reader.readAsDataURL(this.files[0]);
reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function() {
var w = this.width;
var h = this.height;
// alert(w + ' ' + h);
if (w != 155 && h != 200) {
$(document.getElementById('<%=flpPhoto.ClientID%>')).val('');
alert('Invalid Student Photo Size. Please upload photo of resolution 155x200 Pixels.');
}
};
};
});
}
function pageLoad() {
setEvent();
}
</script>
HTML
<asp:FileUpload ID="flpPhoto" TabIndex="52" runat="server" />
Tuesday, July 1, 2014
PDF Creation using iTextSharp - Asp.net C#
Download iTextSharp.dll
Code
protected void Button1_Click(object sender, EventArgs e) { ExportToPDF(); } private void ExportToPDF() { Document MyDocumnet = new Document(PageSize.LEDGER, 30, 30, 30, 30); System.IOMemoryStream MyReport = new System.IOMemoryStream(); PdfWriter writer = PdfWriter.GetInstance(MyDocumnet, MyReport); MyDocumnet.AddAuthor("Aravind"); MyDocumnet.AddSubject("My Firsr Pdf"); MyDocumnet.Open(); string strImagePath = Server.MapPath("StudentsPhoto/6353878428761513791.gif"); iTextSharp.textTable tblHead = new iTextSharp.textTable(1) { WidthPercentage = 50 }; tblHead.Padding = 2; tblHead.Spacing = 0; iTextSharp.textImage imgLogo = iTextSharp.text.Image.GetInstance(strImagePath); Cell cellHead1 = new Cell(imgLogo); cellHead1.HorizontalAlignment = Element.ALIGN_CENTER; cellHead1.VerticalAlignment = Element.ALIGN_MIDDLE; cellHead1.Leading = 8; cellHead1.Colspan = 1; cellHead1.BackgroundColor = Color.LIGHT_GRAY; cellHead1.Border = Rectangle.NO_BORDER; tblHead.AddCell(cellHead1); Cell cellHead2 = new Cell(new Phrase("APPLICATION FOR ADMISSION TO B-TECH COURSE UNDER MANAGEMENT QUOTA",FontFactory.GetFont("Arial Narrow", 14, Font.BOLD + Font.UNDERLINE,Color.BLACK ))); cellHead2.HorizontalAlignment = Element.ALIGN_CENTER; cellHead2.VerticalAlignment = Element.ALIGN_MIDDLE; cellHead2.Leading = 8; cellHead2.Colspan = 1; cellHead2.BackgroundColor = Color.LIGHT_GRAY; cellHead2.Border = Rectangle.NO_BORDER; tblHead.AddCell(cellHead2); Cell cellHead3 = new Cell(new Phrase(" ")); cellHead3.HorizontalAlignment = Element.ALIGN_CENTER; cellHead3.VerticalAlignment = Element.ALIGN_MIDDLE; cellHead3.Leading = 8; cellHead3.Colspan = 1; cellHead3.BackgroundColor = Color.LIGHT_GRAY; cellHead3.Border = Rectangle.NO_BORDER; tblHead.AddCell(cellHead3); cellHead3 = new Cell(new Phrase(" ")); cellHead3.HorizontalAlignment = Element.ALIGN_CENTER; cellHead3.VerticalAlignment = Element.ALIGN_MIDDLE; cellHead3.Leading = 8; cellHead3.Colspan = 1; cellHead3.BackgroundColor = Color.LIGHT_GRAY; cellHead3.Border = Rectangle.NO_BORDER; tblHead.AddCell(cellHead3); MyDocumnet.Add(tblHead); iTextSharp.textTable tblStdDetails = new iTextSharp.textTable(4) { WidthPercentage = 50 }; tblStdDetails.Padding = 3; tblStdDetails.Spacing = 2; AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, "Application Number"); AddCell(tblStdDetails, ": " + lblApplicaionNo.Text); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, "Category"); AddCell(tblStdDetails,": Management"); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, "Name"); AddCell(tblStdDetails, ": " + lblFname.Text + " " + lblMidName.Text + " " + lblLastName.Text); AddCell(tblStdDetails, ""); string strAdress = lblHomeName.Text + "\n " + lblPo.Text + "\n " + lblPlace.Text + "\n " + lblDistrict.Text + "\n " + lblState.Text + "\n " + lblPo.Text; AddCell(tblStdDetails, ""); AddCell(tblStdDetails, "Address"); AddCell(tblStdDetails, ": " + strAdress); AddCell(tblStdDetails,""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, "Date"); AddCell(tblStdDetails, ": " + DateTime.Now.ToShortDateString()); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); AddCell(tblStdDetails, ""); MyDocumnet.Add(tblStdDetails); MyDocumnet.Close(); Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=MyPdf.pdf"); Response.ContentType = "application/pdf"; Response.BinaryWrite(MyReport.ToArray()); Response.End(); } private void AddCell(iTextSharp.textTable tbl, string strData) { Cell MyCell = new Cell(new Phrase(strData, FontFactory.GetFont("Arial Narrow", 12, iTextSharp.text.Font.NORMAL))); MyCell.HorizontalAlignment = Element.ALIGN_LEFT; MyCell.VerticalAlignment = Element.ALIGN_TOP; MyCell.Leading = 8; MyCell.Colspan = 1; MyCell.Border = iTextSharp.text.Rectangle.NO_BORDER; tbl.AddCell(MyCell); }
Subscribe to:
Posts (Atom)