Data binding with ASP.Net application:

Now let’s see how we can bind Student entity graph to GridView in ASP.Net.


First of all , create the ASP.Net web application project. Now drag & drop GridView and EntityDataSource from Data part in Default.aspx:


Databinding with ASP.Net using Entity Framework


Now before you configure EntityDataSource, you have to add connection string in web.config. I have following connection string in web.config:

    <add name="SchoolDBEntities" connectionString="metadata=res://*/DBModel.SchoolDBModel.csdl|res://*/DBModel.SchoolDBModel.ssdl|res://*/DBModel.SchoolDBModel.msl;provider=System.Data.SqlClient;provider 
    connection string=&quot;Data Source=.;Initial Catalog=SchoolDB;Integrated Security=True;MultipleActiveResultSets=True&quot;" 
    providerName="System.Data.EntityClient" />


Now go to design view of Default.aspx and click on configure EntityDataSource.


Select Named Connection from dropdown. This dropdown shows name of connection string in your web.config. We have "SchoolDBEntities”"as connection string name so dropdown will have it.

Databinding with ASP.Net using Entity Framework

Select DefaultContainerName also and click "Next":

Databinding with ASP.Net using Entity Framework

Here, select "Students" EntitySet because we are going to display Student information in the GridView. Click "Finish".


Now we want to display Standard name instead of StandardId. So we have to get the Standard entity which is navigation property in the Student EntitySet. So for that, select EntityDataSource and press F4 to open property window. Here you set Include property value to "Standard":

Databinding with ASP.Net using Entity Framework

Now to configure GridView, click on "Configure GridView and choose "EntityDataSource1" as data source. This will automatically display columns for Students with StandardID. Now to display StandardName instead of StandardId, remove the StandardId column and write following code in TemplateField of GridView:

    <asp:TemplateField HeaderText="Standard Name" SortExpression="StandardName">
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Standard.StandardName") %>'>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("Standard.StandardName") %>'>


Now you are done. Run the project and you will get following display:

Databinding with ASP.Net using Entity Framework