Monday, August 30, 2010

WPF - Custom Tooltip in WPF

Custom Tooltip

In WPF Tooltip property can a host any control like textblock,grid,panel etc.In other words, we can display another window with any number of controls as a tooltip of a control.

Lets create a custom tooltip in wpf

Step 1
Create a WPF application.

Step 2
Add a Button Control on Window,it is look like this


<Grid >
      <Button Margin="12,12,0,0" Name="button1" Height="23" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76">About Me
      </Button>
</Grid>

Step 3
Inside the button control add a tooltip property,it is look like this

<Button Margin="12,12,0,0" Name="button1" Height="23" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76">About Me
       
            <Button.ToolTip>

            </Button.ToolTip>
           
</Button>

Step 4
Inside a tooltip property add the contents like any controls like you want.it is look like this 

<Button.ToolTip>
                <Grid Background="DimGray" Width="400">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50*" />
                        <RowDefinition Height="93*" />
                        <RowDefinition Height="169*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="88*" />
                        <ColumnDefinition Width="190*" />
                    </Grid.ColumnDefinitions>

                    <Label Name="lblHeading" Grid.Column="0" Grid.ColumnSpan="2" Background="Brown" Foreground="white" FontSize="30" >About Me</Label>

                    <Image Name="img" Grid.Row="1" Grid.Column="0" Source="C:\Users\Kishor\Desktop\kishor.png" Stretch="Fill" />

                    <TextBlock Name="txtBAboutME" Padding="10" Grid.Column="1" 
         Grid.Row="1" Grid.RowSpan="2" TextWrapping="Wrap" TextAlignment="Left"
         HorizontalAlignment="Right" VerticalAlignment="Top" Foreground="White">
         My name is kishor naik. I am a freelancer dot net developer.I have a 2+ years experience in .net technology. 
         I have completed my BCA in 2009 with first class with distinction.My aiming to spread good information about developing software, usually on the Microsoft technologies.
         I have been mainly working on window based application and web based application.
                    </TextBlock>
                </Grid>
            </Button.ToolTip> 

Full XAML Code

<Window x:Class="SolWpfCustomTooltip.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Custom Tooltip" Height="300" Width="300" Background="Black">
    
    <Grid >
        <Button Margin="12,12,0,0" Name="button1" Height="23" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76">About Me
        
            <Button.ToolTip>
                <Grid Background="DimGray" Width="400">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50*" />
                        <RowDefinition Height="93*" />
                        <RowDefinition Height="169*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="88*" />
                        <ColumnDefinition Width="190*" />
                    </Grid.ColumnDefinitions>

                    <Label Name="lblHeading" Grid.Column="0" Grid.ColumnSpan="2" Background="Brown" Foreground="white" FontSize="30" >About Me</Label>

                    <Image Name="img" Grid.Row="1" Grid.Column="0" Source="C:\Users\Kishor\Desktop\kishor.png" Stretch="Fill" />

                    <TextBlock Name="txtBAboutME" Padding="10" Grid.Column="1" 
         Grid.Row="1" Grid.RowSpan="2" TextWrapping="Wrap" TextAlignment="Left"
         HorizontalAlignment="Right" VerticalAlignment="Top" Foreground="White">
         My name is kishor naik. I am a freelancer dot net developer.I have a 2+ years experience in .net technology. 
         I have completed my BCA in 2009 with first class with distinction.My aiming to spread good information about developing software, usually on the Microsoft technologies.
         I have been mainly working on window based application and web based application.
                    </TextBlock>
                </Grid>
            </Button.ToolTip> 
            
        </Button>
    </Grid>
</Window>

Output
Move the mouser cursor pointer to the button control


Download
Download Source Code

Sunday, August 29, 2010

LINQ - Linq To Object Part 1

Linq to Objects

This provides the ability to query IEnumerable-based information sources which include arrays, collections, list of objects.

Example on Simple Select Query

Step 1
Create a Web Project.

Step 2
Create a Student Class in App_Code Folder,it is look like this


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;

/// <summary>
/// This is Student class for Storeing Data
/// </summary>
public class Student
{
    #region Property

    public int StudentID
    {
        get;
        set;
    }

    public String FirstName
    {
        get;
        set;
    }

    public String LastName
    {
        get;
        set;
    }

    #endregion
}

Step 3
In default class [default.aspx.cs] write a method for store the student data which return Student list Object.it is look like this

 /// <summary>
    /// Add the Student Data and Store data in list
    /// </summary>
    /// <returns>List</returns>
    private List<Student> AddData()
    {
        try
        {
            List<Student> ListObj = new List<Student>();

            Student Std1 = new Student();
            Std1.StudentID = 1;
            Std1.FirstName = "kishor";
            Std1.LastName = "Naik";


            Student Std2 = new Student();
            Std2.StudentID = 2;
            Std2.FirstName = "kedar";
            Std2.LastName = "deshpande";


            Student Std3 = new Student();
            Std3.StudentID = 3;
            Std3.FirstName = "avinash";
            Std3.LastName = "k";


            Student Std4 = new Student();
            Std4.StudentID = 4;
            Std4.FirstName = "sanket";
            Std4.LastName = "C";


            Student Std5 = new Student();
            Std5.StudentID = 5;
            Std5.FirstName = "Ramdas";
            Std5.LastName = "B";

            Student Std6 = new Student();
            Std6.StudentID = 6;
            Std6.FirstName = "bhavesh";
            Std6.LastName = "pisat";

            ListObj.Add(Std1);
            ListObj.Add(Std2);
            ListObj.Add(Std3);
            ListObj.Add(Std4);
            ListObj.Add(Std5);
            ListObj.Add(Std6);

            return ListObj;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

Step 4
Create a LinqToObject static class in App_code folder,it is look like this

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.Collections.Generic;
using System.Collections;

/// <summary>
/// Summary description for LinqToObject
/// </summary>
public static class LinqToObject
{
    #region Methods
    /// <summary>
    /// Simple Select Query
    /// </summary>
    /// <param name="ListObj">Specify Student List Object</param>
    /// <returns>List</returns>
    public static List<Student> GetStudentData(List<Student> ListObj)
    {
        try
        {
            var Query = (from Q in ListObj
                         select Q).ToList<Student>();

            return Query; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message); 
        }
    }

    /// <summary>
    /// Display Individual filds
    /// </summary>
    /// <param name="ListObj">Specify Student List Object</param>
    /// <returns>Ilist</returns>
    public static IList GetStudentDataSELECTED(List<Student> ListObj)
    {
        try
        {
            var Query = from Q in ListObj
                        select new
                        {
                            Q.FirstName,
                            Q.LastName
                        };

            return Query.ToList();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    #endregion
}
In the above query from operator used like foreach keyword,select is used as projection operator to select the fields from the List Object. These are called Standard Query Operators.

First Method - List<Student> GetStudentData(List<Student> ListObj)

List<Student> which references to collection of objects of an anonymous type which is type equivalent to Student type,Because the Select operator projecting the complete Student object itself.

Second Method - IList GetStudentDataSELECTED(List<Student> ListObj)

In the above query, the variable Query is the type of Ilist references to objects of an anonymous type which contains string FirstName, string LastName as part of the type definition. Because the Select operator projecting only,FirstName, LastName fields of Student object. These two fields are represented as string fields because both are declared as strings as part of the Student type definition. Here we are explicitly using the new keyword to dynamically create an anonymous type. This is mandatory because the select operator is projecting a new type definition.

Step 5
In default class [default.aspx.cs] write a method for binding data to the gridview,it is look like this

.aspx Code

<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GvStudent" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code - Behind

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.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack == false)
            {
                BindGridView(); 
            }

        }
        catch (Exception)
        { 
        }
    }

    #region Methods

    /// <summary>
    /// Add the Student Data and Store data in list
    /// </summary>
    /// <returns>List</returns>
    private List<Student> AddData()
    {
        try
        {
            List<Student> ListObj = new List<Student>();

            Student Std1 = new Student();
            Std1.StudentID = 1;
            Std1.FirstName = "kishor";
            Std1.LastName = "Naik";


            Student Std2 = new Student();
            Std2.StudentID = 2;
            Std2.FirstName = "kedar";
            Std2.LastName = "deshpande";


            Student Std3 = new Student();
            Std3.StudentID = 3;
            Std3.FirstName = "avinash";
            Std3.LastName = "k";


            Student Std4 = new Student();
            Std4.StudentID = 4;
            Std4.FirstName = "sanket";
            Std4.LastName = "C";


            Student Std5 = new Student();
            Std5.StudentID = 5;
            Std5.FirstName = "Ramdas";
            Std5.LastName = "B";

            Student Std6 = new Student();
            Std6.StudentID = 6;
            Std6.FirstName = "bhavesh";
            Std6.LastName = "pisat";

            ListObj.Add(Std1);
            ListObj.Add(Std2);
            ListObj.Add(Std3);
            ListObj.Add(Std4);
            ListObj.Add(Std5);
            ListObj.Add(Std6);

            return ListObj;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    /// <summary>
    /// Bind The Data GridView
    /// </summary>
    private void BindGridView()
    {
        try
        {
            //// Simple Select Query

            //GvStudent.DataSource = LinqToObject.GetStudentData(AddData());
            //GvStudent.DataBind();  


            //// Display Selected Filds
            GvStudent.DataSource = LinqToObject.GetStudentDataSELECTED(AddData());
            GvStudent.DataBind();  

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message); 
        }
    }
    #endregion
}

Run the Project

Note - This above example is continue for next linq to object artical.

Saturday, August 28, 2010

C#.net - Nullable Type

Nullable Type

.NET 2.0 introduced Nullable value types. As you surely know by now, you cannot set a value type to null.

 
int val = null; //this statement give error because it cannot convert to int because it is a non-nullable value type

This can be easily solved by transforming val into a Nullable type:

 
Nullable<int> val = null;

Or you can apply the ? suffix to the variable type:

int? val = null;

How can we check if our nullable value type contains data?
if (val.HasValue)
{
    System.Console.WriteLine("System.Nullable<T> Object have a value");

     // get the value from a nullable object

     System.Console.WriteLine("Value : " + val.Value);
}
else
{
     System.Console.WriteLine("System.Nullable<T> Object dose not have a value");   
}

Full Example on Nullable Type

Step 1
Create a Console application

Step 2
Add the following code snippet on console application,it is look like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NullableType
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int? val = 11;

                // Check the System.Nullable<T> object have a value or not
                if (val.HasValue)
                {
                    System.Console.WriteLine("System.Nullable<T> Object have a value");

                    // get the value from a nullable object

                    System.Console.WriteLine("Value : " + val.Value);
                }
                else
                {
                    System.Console.WriteLine("System.Nullable<T> Object dose not have a value");   
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);    
            }
        }
    }
}

Run the Project

Download
Download Source Code

Friday, August 27, 2010

Asp.net - Send email from gmail by using Asp.net

You can use gmail account to send email from asp.net.This is not straight forward like a simple SMTP email.

Let see how to send email from gmail by using asp.net

Step 1

Create a web Project.

Step 2
Create a following static class on app_code folder.This is following class used for the sending email from gmail account.



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.Web.Mail;

/// <summary>
/// Send Mail from Gamil Account from ASP.net
/// </summary>
public static class GMail
{
    public static Boolean SendGMail(
        String GmailMailID,
        String GmailPassword,
        String To,
        String Subject,
        String Body,
        System.Web.Mail.MailFormat Format,
        String AttachmentPath)
    {
        Boolean Flag = false; 
        try
        {
            System.Web.Mail.MailMessage MyGMail = new System.Web.Mail.MailMessage();
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            MyGMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");
           
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
           
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                GmailMailID);
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 GmailPassword);
            MyGMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");

            MyGMail.From = GmailMailID;
            MyGMail.To = To;
            MyGMail.Subject = Subject;
            MyGMail.BodyFormat = Format;
            MyGMail.Body = Body; 

            if (AttachmentPath.Trim() != String.Empty)
            {
                MailAttachment MAttachment = new MailAttachment(AttachmentPath);
                MyGMail.Attachments.Add(MAttachment);
                MyGMail.Priority = System.Web.Mail.MailPriority.High;
            }

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(MyGMail);

            Flag = true; 
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }

        return Flag; 
    }
}


Step 3

Create a form on .aspx page.it is look like this

<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <h2> Send Mail</h2>
    <table>
    <tr>
    <td style="text-align: right">Gmail Id : </td><td style="text-align: left"><asp:TextBox ID="txtGmailId" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">Password : </td><td style="text-align: left"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">To : </td><td style="text-align: left"><asp:TextBox ID="txtTo" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right">Subject : </td><td style="text-align: left"><asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox></td>
    </tr>
    <tr>
    <td style="text-align: right;vertical-align:top;">Message : </td><td><asp:TextBox  ID="txtBody" 
                        runat="server" TextMode="MultiLine" Height="100px" Width="300px"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td>
    <td align="center">
        <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" Width="100px" /></td>
    </tr>
    </table>
        <asp:Label ID="lblError" ForeColor="red" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Step 4

Call SendGMail function on sendbutton click event,it is look like this

using System;
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.Net.Mail;
using System.Net;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            GMail.SendGMail(txtGmailId.Text.Trim() + "@gmail.com", txtPassword.Text.Trim(), txtTo.Text.Trim(), txtSubject.Text.Trim(),txtBody.Text.Trim(),System.Web.Mail.MailFormat.Text,String.Empty);
            lblError.Text = "Mail sent successfully";  
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;   
        }
    }
}


Note: If you do not want to attach a file with email, specify blank string as String.Empty. 
 
 
Run the Project.

Download
Download Source Code

Wednesday, August 25, 2010

LINQ - What is LINQ

What is LINQ?

LINQ (Language Integrated Query) is the most important new feature of C# 3.0 and .NET 3.5.
LINQ integrated query syntax inside the C# programming language and makes it is possible to access diffferent data source with the same syntax.

Advantage of LINQ

The code size: There are many occasions that the users have to write long sentences for getting a SQL query. LINQ provides relatively short codes in such advanced occasions. It reduces the complexity of the code and makes it much easy for the program to read.



Code equality: One of the most advantages in using LINQ is that its availability over any .NET platform language such as C#.net, VB.NET.

Sample Example on LINQ

Step 1
First create a web project.

Step 2
First Create a student class,it is look like this


   1:   /// <summary>
   2:      /// Store the Student Data
   3:      /// </summary>
   4:      public class Student
   5:      {
   6:          #region Property
   7:          /// <summary>
   8:          /// Get and Set the Student ID
   9:          /// </summary>
  10:          public int StudentID
  11:          {
  12:              get;
  13:              set;
  14:          }
  15:   
  16:          /// <summary>
  17:          /// get and set the Student First Name
  18:          /// </summary>
  19:          public String FirstName
  20:          {
  21:              get;
  22:              set;
  23:          }
  24:   
  25:          /// <summary>
  26:          /// get and set the student Last name
  27:          /// </summary>
  28:          public String LastName
  29:          {
  30:              get;
  31:              set;
  32:          }
  33:   
  34:   
  35:          #endregion
  36:      }
this class used for stored data in student object

Step 3
In default class [default.aspx.cs] write a method for store the student data which return Student list.it is look like this
in this code i intiliaze student object with following data

   1:   /// <summary>
   2:      /// Add the Student Data and Store data in list
   3:      /// </summary>
   4:      /// <returns>List</returns>
   5:      private List<Student> AddData()
   6:      {
   7:          try
   8:          {
   9:              List<Student> ListObj = new List<Student>();
  10:   
  11:              Student Std1 = new Student();
  12:              Std1.StudentID = 1;   
  13:              Std1.FirstName = "kishor";
  14:              Std1.LastName = "Naik";
  15:   
  16:   
  17:              Student Std2 = new Student();
  18:              Std2.StudentID = 2;
  19:              Std2.FirstName = "kedar";
  20:              Std2.LastName = "deshpande";
  21:   
  22:   
  23:              Student Std3 = new Student();
  24:              Std3.StudentID = 3;
  25:              Std3.FirstName = "avinash";
  26:              Std3.LastName = "k";
  27:   
  28:   
  29:              Student Std4 = new Student();
  30:              Std4.StudentID = 4;
  31:              Std4.FirstName = "sanket";
  32:              Std4.LastName = "C";
  33:   
  34:   
  35:              Student Std5 = new Student();
  36:              Std5.StudentID = 5;
  37:              Std5.FirstName = "Ramdas";
  38:              Std5.LastName = "B";
  39:   
  40:              Student Std6 = new Student();
  41:              Std6.StudentID = 6;
  42:              Std6.FirstName = "bhavesh";
  43:              Std6.LastName = "pisat";
  44:   
  45:              ListObj.Add(Std1);
  46:              ListObj.Add(Std2);
  47:              ListObj.Add(Std3);
  48:              ListObj.Add(Std4);
  49:              ListObj.Add(Std5);
  50:              ListObj.Add(Std6);
  51:   
  52:              return ListObj; 
  53:          }
  54:          catch (Exception ex)
  55:          {
  56:              throw new Exception(ex.Message); 
  57:          }
  58:      }

Step 4
Create a LinqDemo class for fetching data from Student ListObject,it is look like this
   1:     public static class LinqDemo
   2:      {
   3:   
   4:          #region Methods
   5:          /// <summary>
   6:          /// Get The Student Data with return list Object
   7:          /// </summary>
   8:          /// <param name="ListObj">Specify the List object</param>
   9:          /// <returns>List</Student></returns>
  10:          public static List<Student> GetStudentDataList(List<Student> ListObj)
  11:          {
  12:              try
  13:              {
  14:                  var Query = from Q in ListObj
  15:                              select Q;
  16:   
  17:                  return Query.ToList<Student>();
  18:              }
  19:              catch (Exception ex)
  20:              {
  21:                  throw new Exception(ex.Message); 
  22:              }
  23:          }
  24:   
  25:          /// <summary>
  26:          /// Get The Student Data with return Ilist
  27:          /// </summary>
  28:          /// <param name="ListObj">Specify the List object</param>
  29:          /// <returns>Ilist</returns>
  30:          public static IList GetStudentDataIList(List<Student> ListObj)
  31:          {
  32:              try
  33:              {
  34:                  var Query=from Q in ListObj
  35:                            select Q;
  36:   
  37:                  return (IList)Query.ToList<Student>();   
  38:              }
  39:              catch (Exception ex)
  40:              {
  41:                  throw new Exception(ex.Message);
  42:              }
  43:          }
  44:          #endregion
  45:          /// <summary>
  46:          /// Get The Student Data with return IQueryable
  47:          /// </summary>
  48:          /// <param name="ListObj">Specify the List object</param>
  49:          /// <returns>IQueryable</returns>
  50:          public static IQueryable GetStudentDataIQueryable(List<Student> ListObj)
  51:          {
  52:              try
  53:              {
  54:                  var Query = from Q in ListObj
  55:                              select Q;
  56:   
  57:                  return Query.AsQueryable();  
  58:              }
  59:              catch (Exception ex)
  60:              {
  61:                  throw new Exception(ex.Message); 
  62:              }
  63:          }
  64:   
  65:          /// <summary>
  66:          /// Get The Student Data with return IEnumerable
  67:          /// </summary>
  68:          /// <param name="Listobj">Specify the List object</param>
  69:          /// <returns>IEnumerable</returns>
  70:          public static IEnumerable<Student> GetStudentDataIEnumerable(List<Student> Listobj)
  71:          {
  72:              try
  73:              {
  74:                  var Query = from Q in Listobj
  75:                              select Q;
  76:   
  77:                  return Query.AsEnumerable<Student>();   
  78:              }
  79:              catch (Exception ex)
  80:              {
  81:                  throw new Exception(ex.Message); 
  82:              }
  83:          }
  84:      }

Step 5
In default class [default.aspx.cs] write a method for binding data to the gridview,it is look like this


   1:   protected void Page_Load(object sender, EventArgs e)
   2:      {
   3:          try
   4:          {
   5:              if (IsPostBack == false)
   6:              {
   7:                  BindData(); 
   8:              }
   9:          }
  10:          catch (Exception)
  11:          { }
  12:      }
  13:     
  14:    private void BindData()
  15:      {
  16:          try
  17:          {
  18:              /// return with List
  19:   
  20:              //gvStudent.DataSource = LinqDemo.GetStudentDataList(AddData());
  21:              //gvStudent.DataBind();
  22:   
  23:              /// return with Ilist
  24:              //gvStudent.DataSource = LinqDemo.GetStudentDataIList(AddData());
  25:              //gvStudent.DataBind(); 
  26:   
  27:              /// return with IQuerable
  28:              //gvStudent.DataSource = LinqDemo.GetStudentDataIQueryable(AddData());
  29:              //gvStudent.DataBind();
  30:   
  31:              /// return with IEnumerable
  32:              gvStudent.DataSource = LinqDemo.GetStudentDataIEnumerable(AddData());
  33:              gvStudent.DataBind();   
  34:          }
  35:          catch (Exception)
  36:          { }
  37:      }
  38:   
  39:    

Final Code
1. default.aspx

   1:  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title>Untitled Page</title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:ScriptManager ID="ScriptManager1" runat="server">
  13:          </asp:ScriptManager>
  14:          
  15:          <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  16:              <ContentTemplate>
  17:                  <asp:GridView ID="gvStudent" runat="server">
  18:                  </asp:GridView>
  19:              </ContentTemplate>
  20:          </asp:UpdatePanel>
  21:      </div>
  22:      </form>
  23:  </body>
  24:  </html>

2.Default.aspx.cs
   1:  using System;
   2:  using System.Configuration;
   3:  using System.Data;
   4:  using System.Linq;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.HtmlControls;
   9:  using System.Web.UI.WebControls;
  10:  using System.Web.UI.WebControls.WebParts;
  11:  using System.Xml.Linq;
  12:  using System.Collections;
  13:  using System.Collections.Generic;
  14:  using System.Runtime.Serialization;
  15:   
  16:  public partial class _Default : System.Web.UI.Page 
  17:  {
  18:      protected void Page_Load(object sender, EventArgs e)
  19:      {
  20:          try
  21:          {
  22:              if (IsPostBack == false)
  23:              {
  24:                  BindData(); 
  25:              }
  26:          }
  27:          catch (Exception)
  28:          { }
  29:      }
  30:   
  31:      #region Method
  32:      /// <summary>
  33:      /// Add the Student Data and Store data in list
  34:      /// </summary>
  35:      /// <returns>List</returns>
  36:      private List<Student> AddData()
  37:      {
  38:          try
  39:          {
  40:              List<Student> ListObj = new List<Student>();
  41:   
  42:              Student Std1 = new Student();
  43:              Std1.StudentID = 1;   
  44:              Std1.FirstName = "kishor";
  45:              Std1.LastName = "Naik";
  46:   
  47:   
  48:              Student Std2 = new Student();
  49:              Std2.StudentID = 2;
  50:              Std2.FirstName = "kedar";
  51:              Std2.LastName = "deshpande";
  52:   
  53:   
  54:              Student Std3 = new Student();
  55:              Std3.StudentID = 3;
  56:              Std3.FirstName = "avinash";
  57:              Std3.LastName = "k";
  58:   
  59:   
  60:              Student Std4 = new Student();
  61:              Std4.StudentID = 4;
  62:              Std4.FirstName = "sanket";
  63:              Std4.LastName = "C";
  64:   
  65:   
  66:              Student Std5 = new Student();
  67:              Std5.StudentID = 5;
  68:              Std5.FirstName = "Ramdas";
  69:              Std5.LastName = "B";
  70:   
  71:              Student Std6 = new Student();
  72:              Std6.StudentID = 6;
  73:              Std6.FirstName = "bhavesh";
  74:              Std6.LastName = "pisat";
  75:   
  76:              ListObj.Add(Std1);
  77:              ListObj.Add(Std2);
  78:              ListObj.Add(Std3);
  79:              ListObj.Add(Std4);
  80:              ListObj.Add(Std5);
  81:              ListObj.Add(Std6);
  82:   
  83:              return ListObj; 
  84:          }
  85:          catch (Exception ex)
  86:          {
  87:              throw new Exception(ex.Message); 
  88:          }
  89:      }
  90:   
  91:      /// <summary>
  92:      /// Bind the Data GridView
  93:      /// </summary>
  94:      private void BindData()
  95:      {
  96:          try
  97:          {
  98:              /// return with List
  99:   
 100:              //gvStudent.DataSource = LinqDemo.GetStudentDataList(AddData());
 101:              //gvStudent.DataBind();
 102:   
 103:              /// return with Ilist
 104:              //gvStudent.DataSource = LinqDemo.GetStudentDataIList(AddData());
 105:              //gvStudent.DataBind(); 
 106:   
 107:              /// return with IQuerable
 108:              //gvStudent.DataSource = LinqDemo.GetStudentDataIQueryable(AddData());
 109:              //gvStudent.DataBind();
 110:   
 111:              /// return with IEnumerable
 112:              gvStudent.DataSource = LinqDemo.GetStudentDataIEnumerable(AddData());
 113:              gvStudent.DataBind();   
 114:          }
 115:          catch (Exception)
 116:          { }
 117:      }
 118:      #endregion
 119:   
 120:      /// <summary>
 121:      /// Store the Student Data
 122:      /// </summary>
 123:      public class Student
 124:      {
 125:          #region Property
 126:          /// <summary>
 127:          /// Get and Set the Student ID
 128:          /// </summary>
 129:          public int StudentID
 130:          {
 131:              get;
 132:              set;
 133:          }
 134:   
 135:          /// <summary>
 136:          /// get and set the Student First Name
 137:          /// </summary>
 138:          public String FirstName
 139:          {
 140:              get;
 141:              set;
 142:          }
 143:   
 144:          /// <summary>
 145:          /// get and set the student Last name
 146:          /// </summary>
 147:          public String LastName
 148:          {
 149:              get;
 150:              set;
 151:          }
 152:   
 153:   
 154:          #endregion
 155:      }
 156:   
 157:      public static class LinqDemo
 158:      {
 159:   
 160:          #region Methods
 161:          /// <summary>
 162:          /// Get The Student Data with return list Object
 163:          /// </summary>
 164:          /// <param name="ListObj">Specify the List object</param>
 165:          /// <returns>List</Student></returns>
 166:          public static List<Student> GetStudentDataList(List<Student> ListObj)
 167:          {
 168:              try
 169:              {
 170:                  var Query = from Q in ListObj
 171:                              select Q;
 172:   
 173:                  return Query.ToList<Student>();
 174:              }
 175:              catch (Exception ex)
 176:              {
 177:                  throw new Exception(ex.Message); 
 178:              }
 179:          }
 180:   
 181:          /// <summary>
 182:          /// Get The Student Data with return Ilist
 183:          /// </summary>
 184:          /// <param name="ListObj">Specify the List object</param>
 185:          /// <returns>Ilist</returns>
 186:          public static IList GetStudentDataIList(List<Student> ListObj)
 187:          {
 188:              try
 189:              {
 190:                  var Query=from Q in ListObj
 191:                            select Q;
 192:   
 193:                  return (IList)Query.ToList<Student>();   
 194:              }
 195:              catch (Exception ex)
 196:              {
 197:                  throw new Exception(ex.Message);
 198:              }
 199:          }
 200:          #endregion
 201:          /// <summary>
 202:          /// Get The Student Data with return IQueryable
 203:          /// </summary>
 204:          /// <param name="ListObj">Specify the List object</param>
 205:          /// <returns>IQueryable</returns>
 206:          public static IQueryable GetStudentDataIQueryable(List<Student> ListObj)
 207:          {
 208:              try
 209:              {
 210:                  var Query = from Q in ListObj
 211:                              select Q;
 212:   
 213:                  return Query.AsQueryable();  
 214:              }
 215:              catch (Exception ex)
 216:              {
 217:                  throw new Exception(ex.Message); 
 218:              }
 219:          }
 220:   
 221:          /// <summary>
 222:          /// Get The Student Data with return IEnumerable
 223:          /// </summary>
 224:          /// <param name="Listobj">Specify the List object</param>
 225:          /// <returns>IEnumerable</returns>
 226:          public static IEnumerable<Student> GetStudentDataIEnumerable(List<Student> Listobj)
 227:          {
 228:              try
 229:              {
 230:                  var Query = from Q in Listobj
 231:                              select Q;
 232:   
 233:                  return Query.AsEnumerable<Student>();   
 234:              }
 235:              catch (Exception ex)
 236:              {
 237:                  throw new Exception(ex.Message); 
 238:              }
 239:          }
 240:      }
 241:  }