In this blog we will insert some data like id, name and amount to database and then display those values in label. Here the amount is converted to words which are displayed in label.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Search_insert_records._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function Validate()
{
var UName=document.getElementById('txt_search');
if((UName.value=='') )
{
alert('Id should not be blank');
document.getElementById("txt_search").focus();
return false;
}
return true;
}
function numeric(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode > 31 && ((charCode >= 48 && charCode <= 57) || charCode == 46))
return true;
else
{
alert('Please Enter Numeric values.');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="ID" Width="150px"></asp:Label>
<asp:TextBox ID="txt_id" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_id" ErrorMessage="Id Required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label2" runat="server" Text="Name" Width="150px"></asp:Label>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txt_name" ErrorMessage="Name Required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label3" runat="server" Text="Amount" Width="150px"></asp:Label>
<asp:TextBox ID="txt_amount" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txt_amount" ErrorMessage="Amount Required"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn_add" runat="server" Text="Add" onclick="btn_add_Click" /><br />
<asp:Label ID="lblmsg" runat="server"></asp:Label><br />
<hr />
<asp:Label ID="Label7" runat="server" Text="Search By Id"></asp:Label>
<asp:TextBox ID="txt_search" runat="server" onkeypress="return numeric(event)"></asp:TextBox>
<br />
<asp:Button ID="btn_search" runat="server" Text="Search"
OnClientClick="Validate()" onclick="btn_search_Click"
CausesValidation="False"/><br />
<asp:Label ID="lbl_id" runat="server" Text=""></asp:Label><br />
<asp:Label ID="lbl_name" runat="server" Text=""></asp:Label><br />
<asp:Label ID="lbl_amount" runat="server" Text=""></asp:Label><br />
<asp:Label ID="lbl_amount_words" runat="server" Text=""></asp:Label><br />
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Search_insert_records
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void btn_add_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "Insert into transactions values(@ID,@name,@amount )";
com.Parameters.Clear();
com.Parameters.AddWithValue("@ID", txt_id.Text);
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@amount ", txt_amount.Text);
if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Successfully Enter!!!";
clear();
}
private void clear()
{
txt_id.Text = "";
txt_name.Text = "";
txt_amount.Text = "";
}
protected void btn_search_Click(object sender, EventArgs e)
{
if (txt_search.Text == "")
{
Response.Write("Id cannot be blank");
}
else
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from transactions where ID = '" + txt_search.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
lbl_id.Text = reader["ID"].ToString();
lbl_name.Text = reader["name"].ToString();
lbl_amount.Text = reader["amount"].ToString();
}
lbl_amount_words.Text = NumberToText.Convert(Convert.ToDecimal(lbl_amount.Text));
reader.Close();
con.Close();
txt_search.Text = "";
}
}
}
}
NumToText.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Search_insert_records
{
static class NumberToText
{
private static string[] _ones =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};
private static string[] _teens =
{
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
private static string[] _tens =
{
"",
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
// US Nnumbering:
private static string[] _thousands =
{
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
};
/// <summary>
/// Converts a numeric value to words suitable for the portion of
/// a check that writes out the amount.
/// </summary>
/// <param name="value">Value to be converted</param>
/// <returns></returns>
public static string Convert(decimal value)
{
string digits, temp;
bool showThousands = false;
bool allZeros = true;
// Use StringBuilder to build result
StringBuilder builder = new StringBuilder();
// Convert integer portion of value to string
digits = ((long)value).ToString();
// Traverse characters in reverse order
for (int i = digits.Length - 1; i >= 0; i--)
{
int ndigit = (int)(digits[i] - '0');
int column = (digits.Length - (i + 1));
// Determine if ones, tens, or hundreds column
switch (column % 3)
{
case 0: // Ones position
showThousands = true;
if (i == 0)
{
// First digit in number (last in loop)
temp = String.Format("{0} ", _ones[ndigit]);
}
else if (digits[i - 1] == '1')
{
// This digit is part of "teen" value
temp = String.Format("{0} ", _teens[ndigit]);
// Skip tens position
i--;
}
else if (ndigit != 0)
{
// Any non-zero digit
temp = String.Format("{0} ", _ones[ndigit]);
}
else
{
// This digit is zero. If digit in tens and hundreds
// column are also zero, don't show "thousands"
temp = String.Empty;
// Test for non-zero digit in this grouping
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
showThousands = true;
else
showThousands = false;
}
// Show "thousands" if non-zero in grouping
if (showThousands)
{
if (column > 0)
{
temp = String.Format("{0}{1}{2}",
temp,
_thousands[column / 3],
allZeros ? " " : ", ");
}
// Indicate non-zero digit encountered
allZeros = false;
}
builder.Insert(0, temp);
break;
case 1: // Tens column
if (ndigit > 0)
{
temp = String.Format("{0}{1}",
_tens[ndigit],
(digits[i + 1] != '0') ? "-" : " ");
builder.Insert(0, temp);
}
break;
case 2: // Hundreds column
if (ndigit > 0)
{
temp = String.Format("{0} hundred ", _ones[ndigit]);
builder.Insert(0, temp);
}
break;
}
}
// Append fractional portion/cents
builder.AppendFormat("and {0:00}/100", (value - (long)value) * 100);
// Capitalize first letter
return String.Format("{0}{1}",
Char.ToUpper(builder[0]),
builder.ToString(1, builder.Length - 1));
}
}
}