Update Data in DataList Using Template With EntityFramework in ASP.Net 4.0

Introduction

This article provides an example of how to update a DataList in ASP.Net. This code example updates data using the Entity Framework. Updating data in a DataList is very easy in ASP.Net, I have used the Entity Framework to Insert, Update and Delete data from a DataList.

Background

For example, a DataList containing a Label, TextBox and Button as a template for displaying and raising events. The DataList in my page is as given below, to do this using the code Drag one DataList Control from a Toolbox and declare their ItemTemplate as given below.

HTML Code

<p class=" "style="margin-bottom: 0.0001pt;"><asp:DataListID="DataList1" runat="server" width="100%"

oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand"

onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound"

onupdatecommand="DataList1_UpdateCommand">

<HeaderTemplate>

<table width="100%">

</HeaderTemplate>

<ItemTemplate>

<tr>

<td style="width:30%">

Address1:

</td>

<td>

<%#Eval("Address1")%>

</td>

</tr>

<tr>

<td>

Address2:

</td>

<td>

<%#Eval("Address2")%>

</td>

</tr>

<tr>

<td style="width:30%">

City:

</td>

<td>

<%#Eval("City")%>

</td>

</tr>

<tr>

<td>

State:

</td>

<td>

<%#Eval("State")%>

</td>

</tr>

<tr>

<td>

Pin Code:

</td>

<td>

<%#Eval("PIN")%>

</td>

</tr>

<tr>

<td>

</td>

<td>

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>

</td>

</tr>

</ItemTemplate>

<EditItemTemplate>

<tr>

<td style="width:10%">

Address1:

</td>

<td>

<asp:TextBox ID="txtAdd1" runat="server" Width="200px" Text='<%#Eval("Address1") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td>

Address2:

</td>

<td>

<asp:TextBox ID="txtAdd2" runat="server" Width="200px" Text='<%#Eval("Address2") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td style="width:30%">

City:

</td>

<td>

<asp:TextBox ID="txtCity" runat="server" Width="200px" Text='<%#Eval("City") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td>

State:

</td>

<td>

<asp:TextBox ID="txtState" runat="server" Width="200px" Text='<%#Eval("State") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td>

Pin Code:

</td>

<td>

<asp:TextBox ID="txtPin" runat="server" Text='<%#Eval("PIN") %>'></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

ErrorMessage="Invalide Pin" ControlToValidate="txtPin"

ValidationExpression="\d{6}">*</asp:RegularExpressionValidator>

</td>

</tr>

<tr>

<td>

</td>

<td>

<asp:Button ID="btnsave" runat="server" Text="Update" EnableViewState="False"

Font-Italic="True" CommandName="Update"></asp:Button>&nbsp&nbsp&nbsp

<asp:Button ID="btncancel" runat="server" Text="Cancel" EnableViewState="False" Font-Italic="True" CommandName="Cancel"></asp:Button>

</td>

</tr>

</EditItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:DataList>


On the page load event bind your
DataList with the database. I have used the Entity Framework to bind to the database as in the following:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            if (Request.QueryString["id"] == null)

            {

                using (OCSEntities ocs = new OCSEntities())

                {

                    ocs.Connection.Open();

                    DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();

                    DataList1.DataBind();

                }

            }

            else

            {

                DataList1.Visible = false;

                pnlnew.Visible = true;

            }

        }

    }

 

Use an ItemCommand Event to specify the Edit item index of the DataList.

For example:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

{

    if (e.CommandName == "Edit")

    {

        DataList1.EditItemIndex = 0;

    }

}


Again, rebind your
DataList to a database table in the editcommand event of the DataList control:

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

{

    //DataList1.EditItemIndex = 0;

    using (OCSEntities ocs = new OCSEntities())

    {

        ocs.Connection.Open();

        DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();

        DataList1.DataBind();

    }

}

Use the updatecommand event to update your database as in the following:
 

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.EditItem)

        {

            using (OCSEntities ocs = new OCSEntities())

            {

                ocs.Connection.Open();

                OCSModel.Address ad = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).First();

                ad.Address1 = ((TextBox)e.Item.FindControl("txtAdd1")).Text;

                ad.Address2 = ((TextBox)e.Item.FindControl("txtAdd2")).Text;

                ad.City = ((TextBox)e.Item.FindControl("txtCity")).Text;

                ad.State = ((TextBox)e.Item.FindControl("txtState")).Text;

                ad.PIN = ((TextBox)e.Item.FindControl("txtPin")).Text;

                ocs.SaveChanges();

                ocs.AcceptAllChanges();

            }

            DataList1.EditItemIndex = -1;

            using (OCSEntities ocs = new OCSEntities())

            {

                ocs.Connection.Open();

                DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();

                DataList1.DataBind();

            }

        }

    }

Summery

In the preceding example we demonstrated how to use a template in the
DataList box and update the database using the Entity Framework.

Up Next
    Ebook Download
    View all
    Learn
    View all