-->

Wednesday, March 19, 2014

Algorithm to implement linear search for Data Structure

Algorithm to implement linear search:

LINEAR SEARCH (arr, n, key)
[ ‘arr’ is one-dimensional array of size ‘n’, key is element to search ]
success <-- FALSE
[Assuming the lower bound of the array as 1]
I<--1
Repeat While (I<=n) AND (success = FALSE)
  If (arr[I] = key) Then:
    success <--TRUE
  [End of If]
  I<--I+1
[End of While]
Return success
Exit.
     In this algorithm initially we assume that the search is unsuccessful by initializing a variable success as FALSE. Then we start the comparison of key from the first element by initializing the index variable I with 1. If the key is equal to first element then success is assigned with TRUE, which terminates the while loop. When the while loop is terminated the algorithm returns success as either TRUE or FALSE depending on the assignment. Similar comparisons are done with all the elements of the array by incrementing the index variable.

How to use LINQ sorting operator in ASP.NET

Introduction

The Sorting operator in LINQ arranges the elements of a sequence based on one or more attributes. You can sort the data with one specific attribute and perform primary sorting on the elements. You can then specify the second sort criterion and sort the elements within the primary sorted group. The different Sorting operators are OrderBy, OrderByDescending, ThenBy, ThenByDescending, and Reverse.
The sorting functionality is achieved using the 'OrderBy operator. Sorting can either be ascending or descending. The default behavior of the OrderBy operator is to sort the data in ascending order. If you want to order your data in descending order, you need to use the OrderByDescending operator.
The syntax of using the OrderBy clause is:
C#
public static OrderedSequence<T> OrderBy<T, K>( this IEnumerable<T> source, Function<T, K> keySelector);

Similarly, you can use the OrderByDescending clause to sort the string in a descending order, as shown in the following code:

C#
public static OrderedSequence<T> OrderByDesceridinq<T, K>( this IEnumerable<T> source, Function<T, K> keySeIector);

Lets take an simple example 

Step-1 : Add one ListBox and two Button control on design window
   <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Ascending" 
            onclick="Button1_Click" />
    &nbsp;<asp:Button ID="Button2" runat="server" Text="Descending " 
            onclick="Button2_Click" />
    </div>
    </form>
Step-2 : Handle business logic code on Button Click event.
Step-3 : Create double type array with some values.

 double [] dotprogramming = { 1, 2, 3, 4, 5, 6 };

Step-4 : Select filter type using OfType<string>(); extension IEnumerable.
Step-5 : Add filtered value into ListBox.

Output of given below code

How to use LINQ sorting operator in ASP.NET

How to use LINQ sorting operator in ASP.NET
public partial class Default8 : System.Web.UI.Page
{
    double [] dotprogramming = { 1, 2, 3, 4, 5, 6 };

    
    protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        var query = from d in dotprogramming
                    orderby d
                    select d;
        foreach (var item in query)
        {
            ListBox1.Items.Add(item.ToString() );
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        var query = from d in dotprogramming
                    orderby d descending
                    select d;
        foreach (var item in query)
        {
            ListBox1.Items.Add(item.ToString());
        }

    }
}

Example of Bottom Text alignment of Gridview in asp.net

Initial Steps for viewers

Step-1 : Open Visual Studio IDE
Step-2 : Add New Web form into your project
Step-3 : Open Design page for adding control on it.
Step-4 : You can change text alignment of header row text.
Example of Bottom Text alignment of Gridview in asp.net


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>
  <HeaderStyle VerticalAlign="Bottom" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
 </div>
    </form>
</body>
</html>
Example of Bottom Text alignment of Gridview in asp.net

Searching different types for Data Structure

We study here different types of searching like linear search and binary search applied on linear data structure, lists (array and linked list).

Linear search:
In this type search each and every element of the data structure (array or linked list) is compared with the key element till either the element is found or the list is over. The linear searching technique is used when there is no availability of direct address of the elements. Even though the address of the element is directly available, the application of linear search also depends on the order of elements stored in a list. So linear search can be applied either to one-dimensional array where the elements are not necessarily arranged in any order (although the address of any element is available directly) or to linked list where the address of any element is directly not available (although the elements may be ordered).
 
Binary search:
    In this type of search the elements in the list must be arranged in an order and the address of any element must be available directly. The direct availability of any element ’s address is only possible in case of one dimensional array can be arranged in order. So the binary search can only be applied to one dimensional array. In case of linked list binary search can not be applied because, even the elements may be arranged in an order, but the address of any element is not available directly.

Linear search applied to one dimensional array:
           Understanding and implementation of linear search to single or one dimensional array is simple. In any one-dimensional array the key to be searched is simply compared with all the elements of the array one by one till the last element. In between if the search is successful means if the key is equal to one of the elements then stop the comparison and return success. When comparison of the key with all the elements is over then return non-success. Usually when searching is applied to the list we assume that the elements are unique. So to apply linear search the elements of the array may not necessarily be in ascending or descending order. Remember that even though every element’s address is directly available, the linear search always performs comparison of key with the first element only and runs till the last element.

Tuesday, March 18, 2014

How to use LINQ Filtering operators in ASP.NET

Introduction

Filtering, as the name suggests, refers to the operation of filtering the result set so that it contains only those elements that satisfy a specified given condition. The Where clause is used as the Filtering operator in LINQ. The Where clause is used to filter a sequence based on a given condition. The syntax of using the Where clause with LINQ is:

public static IEnumerable<T> where <T> (this IEnumerable<T> source, Function<T, bool> predicate);

The Where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. For example, you can use the Where clause to filter numbers except those that are less than 5.

Lets take an simple example 

Step-1 : Add one ListBox and Button control on design window
.
<form id="form1" runat="server">
    <div>        
        <span class="style1"><strong>Example of Filtering type operator in LINQ</strong></span><br />
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Add item to the List" />        
        <br />
    </div>
    </form>
Step-2 : Handle business logic code on Button Click event.
Step-3 : Create Object type array with different type values.

  object[] data = { 10, "ten", 20, "twenty", 30, "thirty" };

Step-4 : Select filter type using OfType<string>(); extension IEnumerable.
Step-5 : Add filtered value into ListBox.

Output of given below code

How to use LINQ Filtering operators in ASP.NET
#region filtertype_operator
    protected void Button1_Click(object sender, EventArgs e)
    {
        object[] data = { 10, "ten", 20, "twenty", 30, "thirty" };
        var myquery = data.OfType<string>();
        foreach (var item in myquery)
        {
            ListBox1.Items.Add(item);
        }

    }
    #endregion

GridView header row Border style as Double example in asp.net

 Initial Steps for viewers

Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Open Design page for adding control onit.
Step-4 : You can change Border style of header as Double by Property window.
GridView header row Border style as Double example in asp.net

<!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">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

   <HeaderStyle BorderStyle="Double" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>

Code output:


GridView header row Border style as Double example in asp.net

Sunday, March 16, 2014

Change Header border style of Grid View in ASP.NET

 Initial Steps for viewers

Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Open Design page for adding control onit.
Step-4 : You can change Border Style of Header row using property window

Change Header border style of Grid View in ASP.NET

<!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">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

 <HeaderStyle BorderStyle="Groove" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>

Code output:

Output of  Header border style of Grid View
© Copyright 2013 Computer Programming | All Right Reserved