Friday 10 January 2014

Autocomplete using ajax.

Aspx Page
-----------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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>AutoComplete Box with jQuery</title>
<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">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/GetAutoCompleteData",
                    data: "{'name':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="demo">
<div class="ui-widget">
<label for="tbAuto">Enter UserName: </label>
    <asp:TextBox ID="txtSearch" runat="server" class="autosuggest"></asp:TextBox>
</div>
</form>
</body>
</html>

.VB Page
----------
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Web.Services

Partial Class _Default
    Inherits System.Web.UI.Page

    <WebMethod()> _
    Public Shared Function GetAutoCompleteData(ByVal name As String) As List(Of String)
        Dim result As New List(Of String)()
        Using con As New SqlConnection("Data Source=xxx;Initial Catalog=xxx;User ID=xx;Password=xxx;trusted_Connection=false; Connect timeout=0;")
            Using cmd As New SqlCommand("select Country_Name from All_Countries where Country_Name LIKE @SearchText+'%'", con)
                con.Open()
                cmd.Parameters.AddWithValue("@SearchText", name)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read()
                    result.Add(dr("Country_Name").ToString())
                End While
                Return result
            End Using
        End Using
    End Function
End Class

Monday 6 January 2014

Show Column total in Gridview footer

Aspx Page:
------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" ShowFooter="true"
            runat="server" AllowPaging="True">
            <Columns>
                <asp:TemplateField HeaderText="ProductID">
                    <ItemTemplate>
                        <asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ProductName">
                    <ItemTemplate>
                        <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" Font-Bold="true" runat="server" Text="Total"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="UnitPrice">
                    <ItemTemplate>
                        <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotalUnitPrice" Font-Bold="true" runat="server"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="UnitsInStock">
                    <ItemTemplate>
                        <asp:Label ID="lblUnitsInStock" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotalUnitsInStock" Font-Bold="true" runat="server"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>


.VB Page
--------
Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        GridView1.DataSource = ProductsData()
        GridView1.DataBind()
    End Sub

    Public Function ProductsData() As DataSet
        Dim text As String = "select   ProductID, ProductName, UnitPrice, UnitsInStock  from ProductMaster"

        Dim connString As String = "Data Source=YourServer;Initial Catalog=your database;User ID=***;Password=***;trusted_Connection=false; Connect timeout=0;"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        conn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        conn.Close()
        Return ds
    End Function

    Private totalUnitPrice As Decimal = 0
    Private totalUnitInstock As Integer = 0

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            totalUnitPrice += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"))
            totalUnitInstock += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"))
        End If
        If e.Row.RowType = DataControlRowType.Footer Then
            Dim lblGTUnitPrice As Label = DirectCast(e.Row.FindControl("lblTotalUnitPrice"), Label)
            Dim lblGTUnitInStock As Label = DirectCast(e.Row.FindControl("lblTotalUnitsInStock"), Label)
            lblGTUnitPrice.Text = totalUnitPrice.ToString()
            lblGTUnitInStock.Text = totalUnitInstock.ToString()
        End If
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = ProductsData()
        GridView1.DataBind()
    End Sub
End Class

Thursday 2 January 2014

Differences between compiler and interpreter

The main differences between compiler and interpreter are listed below:
1        The interpreter takes one statement then translates it and executes it and then takes another statement. While the compiler translates the entire program in one go and then executes it.
2        Compiler generates the error report after the translation of the entire page while an interpreter will stop the translation after it gets the first error.
3         Compiler takes a larger amount of time in analyzing and processing the high level language code comparatively interpreter takes lesser time in the same process.
4         Besides the processing and analyzing time the overall execution time of a code is faster for compiler relative to the interpreter.