How to display record of database in gridview using linq in asp.net using c#

Posted by Unknown On Wednesday, 9 October 2013 3 comments
Hi, I am mentioned in this post that how display a record in gridview from database using linq.
For this purpose you have to follow the below instruction.
First of all see my How to Insert record using linq.
Now I design the page up to textbox and button, now below that take a grid view control and design it as follow.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqSimple.aspx.cs" Inherits="LinqSimple" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"></head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>
                <asp:Label ID="lblname" runat="server" Text="Student Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
             <td>
                <asp:Label ID="lblstd" runat="server" Text="Standard"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtstd" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
           <td>
               <asp:Label ID="lblcnt" runat="server" Text="ContactNo"></asp:Label>
           </td>
            <td>
                <asp:TextBox ID="txtcnt" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Label ID="lblresult" runat="server" Text="Result"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtresult" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />
            </td>
        </tr>
    </table>
       <asp:GridView ID="gv_student" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="name" HeaderText="Student Name" />
                <asp:BoundField DataField="std" HeaderText="Standard" />
                <asp:BoundField DataField="cntno" HeaderText="ContactNo" />
                <asp:BoundField DataField="result" HeaderText="Result" />
            </Columns>
        </asp:GridView>
     </div>
    </form>
</body>
</html>

Your page looks like below.

Now write the code in code behind file as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LinqSimple : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        using (LinqDemoDataContext database = new LinqDemoDataContext())
        {
            var slist = (from stud in database.Students
                         select new 
                         {
                             stud.name,
                             stud.std,
                             stud.cntno,
                             stud.result
                         }).ToList();
            gv_student.DataSource = slist;
            gv_student.DataBind();
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        using (LinqDemoDataContext database = new LinqDemoDataContext())
        {
            Student s = new Student()
            {
                name = txtname.Text,
                std = txtstd.Text,
                cntno = Convert.ToInt32(txtcnt.Text),
                result = txtresult.Text
            };
            database.Students.InsertOnSubmit(s);
            database.SubmitChanges();
            BindGrid();
        }
    }
}

Now when the page is load it display the record, and also after inserting a record it display in gridview.
Hope it will help you; I’m appreciate your comment and likes.


3 comments:

Gaurav said...

Great Post..
Thanks buddy

Unknown said...

Could you tell me what code goes into "LinqDemoDataContext" class?

Unknown said...

Dear Fabio Guerreiro "LinqDemoDataContext" class is the dbml file that we take for linq and in which we have drag and drop the our tables like in entity data model. even if you have any doubt you can ask me , your comments is appreciated here.

Post a Comment