Pages

Ruchi Tech

Monday, 8 October 2012

How to create AutoComplete TextBox with ASP.NET and jQuery UI

This article explains how to use JQuery UI AutoComplete widget

AutoComplete - A feature that suggests text automatically based on the first few characters that a user types.

 Step:1 Add following code in "default.aspx" page


<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> 
<script type="text/javascript">

$(function () {
   $("[id$=txtAuto]").autocomplete({
   source: function (request, response) {

   $.ajax({
      url: "NameList.asmx/GetNameList",
 
      data: "{ 'Name': '" + request.term + "' }",
      dataType: "json",
      type: "POST", 
      contentType: "application/json; charset=utf-8",
      async: true,
      success: function (data) {               
                   var Details = [];
                   for (i = 0; i < data.d.length; i++) {
                         Details[i] = data.d[i].Name;
                                 } response(Details);
                        },
      error: function (result) {

                         }
          });
       }
    });
});

</script>


Add HTML Code:


<div class="demo">
    <div class="ui-widget">
       <label for="txtAuto">Enter Name: </label>
       <asp:TextBox ID="txtAuto" runat="server">
       </asp:TextBox>
    </div>
</div>


Step:2 Add Web Service and write following code in it.


[WebMethod]
public List<UserNameList> GetNameList(string Name)
{ 
  var emp = new UserNameList();  
  var fetchName = emp.GetEmpList()
  .Where(m => m.Name.ToLower().StartsWith(Name.ToLower()));
  return fetchName.ToList();       
}

public class UserNameList
{
  public int ID { get; set; } 

  public string Name { get; set; }
  public List<UserNameList> GetEmpList() 
  {
      List<UserNameList> emp = new List<UserNameList>();

      emp.Add(new UserNameList() { ID = 1, Name = "Arjun" });  
      emp.Add(new UserNameList() { ID = 2, Name = "Aaryan" });
      emp.Add(new UserNameList() { ID = 3, Name = "Anoop" });
  

      emp.Add(new UserNameList() { ID = 4, Name = "Bob" });
      emp.Add(new UserNameList() { ID = 5, Name = "Boby" });
      emp.Add(new UserNameList() { ID = 6, Name = "Cristen" });  
      emp.Add(new UserNameList() { ID = 7, Name = "Cris" });
      return emp;

  }
}


 Now run the application and check the output.It would be like this
You can download whole code here AutoCompleteTextBox


Sunday, 23 September 2012

SQL Server Fucntion to Split Comma-Seperated Strings into table

Split function in general would have comma-separated string value to be split into individual strings.

The below Split function is Table-valued function which would help us splitting comma-separated (or any other delimiter value) string to individual string.

CREATE  FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000)) 
as  
begin 
declare @idx int
declare @slice varchar(8000) 
select @idx =
if len(@String)<1 or @String is null return 
while @idx!=
begin 
set @idx = charindex(@Delimiter,@String) 
if @idx!=
set @slice = left(@String,@idx - 1) 
else 
set @slice = @String 
if(len(@slice)>0)
insert into @temptable(Items) values(@slice) 
set @String = right(@String,len(@String) - @idx) 
if len(@String) = 0 break 
end 
return

split function can be Used as

       select * from dbo.split ( '9899999999,9877889876,9865489678' , ',' )


would return



Hope this helps.

Monday, 17 September 2012

How to Convert a numbers to words in asp.net

This article explains about how to convert a numeric value to words.

For Example: Numeric: 500
                       In Words: Five hundred Only


Write a Code:

staticclass NumberToWord
{ 
  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"
  };
 private static string[] _thousands =
  {
     "",
     "thousand",
     "million",
     "billion",
     "trillion",
     "quadrillion"
  };

 public static string Convert(decimal value)
  { 

    string digits, temp;
    bool showThousands = false;
    bool allZeros = true;           
    StringBuilder builder = new StringBuilder();
    digits = ((long)value).ToString(); 

    for(int i = digits.Length-1; i >= 0; i--)
    {

      int ndigit = (int)(digits[i] - '0');
      int column = (digits.Length - (i + 1));
      switch (column % 3)
      { 

  case 0:   
      
showThousands = true; 

       if (i == 0)
        
{
           temp =
String.Format("{0} ", _ones[ndigit]);
        
}
      
else if (digits[i - 1] == '1')
        
{
           temp =
String.Format("{0} ", _teens[ndigit]);
          
i--;
         }
      
else if (ndigit != 0)
       
{
          temp =
String.Format("{0} ", _ones[ndigit]);
       
}
      
else
       
{
          temp =
String.Empty;
         
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
         
showThousands = true;
         
else
         
showThousands = false;
       
}
      
if (showThousands)
      
{
       
if (column > 0)
       
{
         temp =
String.Format("{0}{1}{2}",
               
temp,_thousands[column / 3], allZeros ? " " : " ");
        }

        allZeros =
false;
      
}
       builder.Insert(0, temp);
      
break;

 
case 1:
        if (ndigit > 0)
        {
         temp =
String.Format("{0}{1}",_tens[ndigit],

         (digits[i + 1] != '0') ? "-" : " ");builder.Insert(0, temp);
       }
      
break;
 

case 2:
      if (ndigit > 0)
        {
         temp =
String.Format("{0} hundred ", _ones[ndigit]);
                    builder.Insert(0, temp);
       }
      
break;
     }}
   builder.AppendFormat(
"only", (value - (long)value) * 100);
   return String.Format("{0}{1}",
   Char.ToUpper(builder[0]),
   builder.ToString(1, builder.Length - 1));
  }
 

}

 

Add following code on Button_Click event

decimal number;
if (!string.IsNullOrEmpty(txtAmount.Text) && decimal.TryParse(txtAmount.Text.Trim(), out number))
{
   lblAmountChar.Text = "(" + NumberToWord.Convert(number) + ")";
}

It convert given number to words. Thanks.