Saturday, September 5, 2015

Asp.net Entity Framework Fetch records and display in Gridview

In this article I am going to explain how to fetch the records from Sql server database and display in Gridview using Entity Framework in Asp.net

Description:

In this tutorial select the records from Sql server database using database first approach. I have created a table Movie in database and having records in it. I am going to display the records in Gridview data control.

Implementation:
To set up the ADO.net entity framework project read the article How to set upADO.net Entity Framework project or website in asp.net.


After that add a webform to project/website. Drag and drop the Gridview control from toolbox to webform.


HTML Markup of webform:
  <fieldset style="width:500px;">
            <legend>Entity Framework Tutorial</legend>              
         <asp:GridView ID="grdmovie" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
               <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
               <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Movie Name" >
                     <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                     <asp:BoundField DataField="Genre" HeaderText="Genre"> <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                     <asp:BoundField DataField="Cost" HeaderText="Budget (In Crore)"> <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                     <asp:ImageField DataImageUrlField="Poster" HeaderText="Poster" ControlStyle-Width="200px">
<ControlStyle Width="200px"></ControlStyle>
                    </asp:ImageField>
                </Columns>
               <EditRowStyle BackColor="#999999" />
               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
               <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
               <SortedAscendingCellStyle BackColor="#E9E7E2" />
               <SortedAscendingHeaderStyle BackColor="#506C8C" />
               <SortedDescendingCellStyle BackColor="#FFFDF8" />
               <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
         </asp:GridView>
        </fieldset>

Create the object of Entity connection
C#:
DemoEntities db = new DemoEntities();

VB:
Dim db As New DemoEntities

Create a function to bind gridview and call it on page load.
C#:
protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGrid();
        }
    }
public void BindGrid()
    {
        var bindgrid = db.Movies.Select(m => m);
        grdmovie.DataSource = bindgrid.ToList();
        grdmovie.DataBind();
    }

VB:
  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Public Sub BindGrid()
        Dim bindgrid = db.Movies.[Select](Function(m) m)
        grdmovie.DataSource = bindgrid.ToList()
        grdmovie.DataBind()
    End Sub


Build the project and run. 
Demo:
Asp.net Entity Framework Fetch records and display in Gridview

  In this article we have learn to how to fetch and display record using Entity Framework 6.0 in asp.net Visual studio 2013I hope you enjoyed this article.

No comments:

Post a Comment