Data binding with ASP.Net application:

In this section how we can bind Student entity graph to GridView in ASP.Net will be demonstrated.

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


Now before you configure EntityDataSource, you have to add a connection string in the web.config. I have the 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 a connection string name so the dropdown will contain it.


Select DefaultContainerName also and click "Next":


In the next window, 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 a navigation property in the Student EntitySet. So for that, select EntityDataSource and press F4 to open the property window. Here you can set Include property value to "Standard":


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

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

                    

That is everything. Run the project and you will get following display: