-->

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

Searching for Data Structure in C Programming

Searching 

Searching is an operation that is applied on any of the data structure looking for the existence of an item or element within the same. So searching is simply a Boolean operation that returns TRUE if the element looking for exists in the data structure, otherwise to return FALSE. In almost every area of computing field searching is found. So understanding and implementation of searching is must for a computer programmer along with learning of data structure. Following are the few examples of searching:
. Searching a file in a directory or folder
. Searching name in a list of names
. Searching an address of a web site
. Searching a free partition to allocate memory to process
. Searching a free space in disk to allocate for a file
Basically searching operation is applied to every data structure to find out the existence of a member or element within it. The type of searching used for implementation depends on:
. Direct availability of index or address of elements
. Order of elements that are stored in 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).

Double Ended Queue (DEQUE) in 'C'

Double Ended Queue (DEQUE)

DEQUE (pronounced as deck), Double Ended Queue, is a type of Queue in which additions and deletion are deletions are done from both the ends. So it is called as Double Ended Queue.

DEQUEs are of two types.

1. Input restricted DEQUE:
                        In this type of Double Ended Queue, the ADD (insertion) Operation is restricted to one end and the DELETE operation is done from both the ends. Only REAR is used for insertion operation and both FORNT and REAR are used for deletion operation. When an item is added REAR is updated accordingly and the item is inserted at REAR index. When an item is deleted from the FRONT end, FRONT is updated accordingly FRONT<--FRONT+1. When an item is deleted from REAR end, REAR is updated as REAR<--REAR-1.

2. Output restricted DEQUE:
                        It is opposite of Input restricted DEQUE. In this type Double Ended Queue, the DELETE operation is restricted to one end the insertion (ADD) operation is done to both the ends. Only FRONT is used for DELETE operation and both FRONT and REAR are used for ADD operation. When an item is added to the FRONT end, FRONT is updated accordingly FRONT<--FRONT-1, and the item is inserted at FRONT index. When an item is added to the item is inserted at REAR Index.

Saturday, March 15, 2014

How to access cell value from DataTable in ASP.NET

Introduction

Suppose, i have Database table with some rows and columns. Look like given below

How to access cell value from DataTable in ASP.NET

And i want to access cell value of it. Now, first discuss about DataTable, It is container, It Contains multiple Data Columns and multiple Data Rows. It is Main part of ADO.NET library. In this program, we will retrieve cell value of DataTable. Now, first load DataTable with any datasource using load( ) method. After that you can retrieve cell value of it easily.

Now Let's go for an example

Above mentioned snapshot, If we want to access jacob text from given table then consider this table as 2D array. Now, you can access this text from table using itemArray property of DataRow class. Like

string name =  instance of DataTable.Rows[0].ItemArray[1].ToString();
 Similarly, if want to access whole data of single column then must use loop, such as

for (int i = 0; i < table .Rows .Count; i++)
                {
                    
            string  name1 =   instance of DataTable.Rows[i].ItemArray[1].ToString(); +"<br/>";  
                    
                   
                }

Output of given mentioned code

How to access cell value from DataTable in ASP.NET


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default5 : System.Web.UI.Page
{
    string name1=string.Empty ;
    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
            con.Open ();
            using(SqlCommand cmd = new SqlCommand ())
{
                cmd.CommandText = "select * from Register";
                cmd.Connection =con;
                SqlDataReader rd=cmd.ExecuteReader ();
                DataTable table;
                table = new DataTable();
                table.Load(rd);
                for (int i = 0; i < table .Rows .Count; i++)
                {
                    
              name1 +=  table.Rows[i].ItemArray[1].ToString() +"<br/>";  
                    
                   
                }
                Label1.Text = name1;
}
        }

    }

    }

C function for an ADD operation

C function for an ADD operation:


void addlq(QUEUE *front, QUEUE *rear, int item)
{
QUEUE *temp = (QUEUE*) malloc (sizeof(QUEUE));
temp-->i=item;
temp-->link=0;
if (rear= =0)
{ rear=temp; front=temp; }
else
{ rear-->link=temp; rear=temp; }
}

To implement a Linear Queue using Linked List, in C, a self-referential struct QUEUE can be used similar to Linked List implemented STACK. The user define data type struct QUEUE contain one field to store the data say int i and the other field QUEUE *link to store the address of next node.
struct QUEUE
{
int i;
QUEUE *link;
};


© Copyright 2013 Computer Programming | All Right Reserved