Results 1 to 1 of 1

Thread: ASP.NET FormView CRUD (Create, Update, Delete) with Entity Framework

  1. #1

    Thread Starter
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    ASP.NET FormView CRUD (Create, Update, Delete) with Entity Framework

    This example integrates Entity Framework instead of DataSource wizard controls which
    is covered on most tutorials available for this control.

    To start with, execute the T-sql script and create an asp.net project. In your project add a WebForm called FormViewDemo and an ADO.NET Entity Data Model BooksEntities which points to the created table called BookDetails. Then use the codes below to complete this application.

    SQL Code:
    sql Code:
    1. CREATE TABLE [dbo].[BookDetails](
    2.     [BookSerialNo] [int] IDENTITY(1,1) NOT NULL,
    3.     [BookISBN] [nchar](15) NULL,
    4.     [BookTitle] [varchar](120) NULL,
    5.     [BookAuthor] [varchar](60) NULL,
    6.     [BookPublisher] [varchar](50) NULL,
    7.     [BookCategory] [varchar](20) NULL,
    8. PRIMARY KEY CLUSTERED
    9. (
    10.     [BookSerialNo] ASC
    11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    12. ) ON [PRIMARY]
    13.  
    14.  
    15. GO
    16.  
    17.  
    18. SET ANSI_PADDING OFF
    19. GO

    ASPX Markup (Add inside the <form> element)
    html Code:
    1. <asp:FormView ID="FormViewBookDetails" runat="server" AllowPaging="True" DataKeyNames="BookSerialNo" GridLines="Both" ClientIDMode="Static"
    2.                   OnItemCommand="FormViewBookDetails_ItemCommand" OnPageIndexChanging="FormViewBookDetails_PageIndexChanging"
    3.                   OnModeChanging="FormViewBookDetails_ModeChanging" OnItemInserting="FormViewBookDetails_ItemInserting"
    4.                   OnItemDeleting="FormViewBookDetails_ItemDeleting" PagerSettings-Mode="NumericFirstLast" OnItemUpdating="FormViewBookDetails_ItemUpdating">
    5.                   <headertemplate>
    6.                       <table class="" id="tblHeader">
    7.                         <tr>
    8.                           <td colspan="2">
    9.                               <div id="Header">
    10.                                   <asp:label id="lbl" Text="GHK Bookshop FormView Create-Update-Delete Demo" runat="server"/>
    11.                               </div>                                            
    12.                           </td>
    13.                         </tr>
    14.                       </table>
    15.                 </headertemplate>
    16.                 <EditItemTemplate>
    17.                     <table id="tblEdit">
    18.                         <tr>
    19.                             <td >Serial # : </td>
    20.                             <td>
    21.                                 <asp:Label ID="lblBookSerialNo" runat="server" Text='<%# Eval("BookSerialNo") %>' />
    22.                             </td>
    23.                         </tr>
    24.                         <tr>
    25.                             <td >ISBN : </td>
    26.                             <td>
    27.                                 <asp:TextBox ID="txtBookISBN" runat="server" required="required"  Text='<%# Bind("BookISBN") %>'></asp:TextBox>
    28.                             </td>
    29.                         </tr>
    30.                         <tr>
    31.                             <td >Title : </td>
    32.                             <td>
    33.                                 <asp:TextBox ID="txtBookTitle" runat="server"  required="required" Text='<%# Bind("BookTitle") %>'></asp:TextBox>
    34.                             </td>
    35.                         </tr>
    36.                         <tr>
    37.                             <td >Author : </td>
    38.                             <td>
    39.                                 <asp:TextBox ID="txtBookAuthor" runat="server" required="required"  Text='<%# Bind("BookAuthor") %>'></asp:TextBox>
    40.                             </td>
    41.                         </tr>
    42.                         <tr>
    43.                            <td >Publisher : </td>
    44.                             <td>
    45.                                 <asp:TextBox ID="txtBookPublisher" runat="server" required="required"  Text='<%# Bind("BookPublisher") %>'></asp:TextBox>
    46.                             </td>
    47.                         </tr>
    48.                         <tr>
    49.                            <td >Category : </td>
    50.                             <td>
    51.                                 <asp:TextBox ID="txtBookCategory" runat="server" required="required"  Text='<%# Bind("BookCategory") %>'></asp:TextBox>
    52.                             </td>
    53.                         </tr>
    54.                         <tr>
    55.                             <td colspan="2">
    56.                                 <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />
    57.                                 <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" UseSubmitBehavior="false" CausesValidation="false"/>
    58.                             </td>
    59.                         </tr>
    60.                     </table>
    61.                 </EditItemTemplate>
    62.                 <ItemTemplate>
    63.                     <table id="tblItem">
    64.                         <tr>
    65.                             <td>Serial # : </td>
    66.                             <td>
    67.                                 <asp:Label ID="EmployeeIDLabel" runat="server" Text='<%# Eval("BookSerialNo") %>' />
    68.                             </td>
    69.                         </tr>
    70.                         <tr>
    71.                             <td >ISBN : </td>
    72.                             <td>
    73.                                 <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("BookISBN") %>' />
    74.                             </td>
    75.                         </tr>
    76.                         <tr>
    77.                             <td >Title : </td>
    78.                             <td>
    79.                                 <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("BookTitle") %>' />
    80.                             </td>
    81.                         </tr>
    82.                         <tr>
    83.                             <td >Author : </td>
    84.                             <td>
    85.                                 <asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("BookAuthor") %>' />
    86.                             </td>
    87.                         </tr>
    88.                         <tr>
    89.                             <td>Publisher :</td>
    90.                             <td>
    91.                                 <asp:Label ID="NotesLabel" runat="server" Text='<%# Bind("BookPublisher") %>' />
    92.                             </td>
    93.                         </tr>
    94.                         <tr>
    95.                             <td>Category :</td>
    96.                             <td>
    97.                                 <asp:Label ID="Label1" runat="server" Text='<%# Bind("BookCategory") %>' />
    98.                             </td>
    99.                         </tr>
    100.                         <tr>
    101.                             <td colspan="2">
    102.                                 <asp:Button ID="btnInsert" runat="server" Text="Add" CommandName="New" />
    103.                                 <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" UseSubmitBehavior="false"/>                        
    104.                                 <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />
    105.                             </td>
    106.                         </tr>
    107.                     </table>
    108.                 </ItemTemplate>
    109.                 <InsertItemTemplate>
    110.                       <table id="tblInsert">
    111.                         <tr>
    112.                             <td >ISBN : </td>
    113.                             <td>
    114.                                 <asp:TextBox ID="txtInsertBookISBN"  required="required" runat="server" Text='<%# Bind("BookISBN") %>'></asp:TextBox>
    115.                             </td>
    116.                         </tr>
    117.                         <tr>
    118.                             <td >Title : </td>
    119.                             <td>
    120.                                 <asp:TextBox ID="txtInsertBookTitle" required="required"  runat="server" Text='<%# Bind("BookTitle") %>'></asp:TextBox>
    121.                             </td>
    122.                         </tr>
    123.                         <tr>
    124.                             <td >Author : </td>
    125.                             <td>
    126.                                 <asp:TextBox ID="txtInsertBookAuthor" required="required"  runat="server" Text='<%# Bind("BookAuthor") %>'></asp:TextBox>
    127.                             </td>
    128.                         </tr>
    129.                         <tr>
    130.                            <td >Publisher : </td>
    131.                             <td>
    132.                                 <asp:TextBox ID="txtInsertBookPublisher" required="required"  runat="server" Text='<%# Bind("BookPublisher") %>'></asp:TextBox>
    133.                             </td>
    134.                         </tr>
    135.                         <tr>
    136.                            <td >Category : </td>
    137.                             <td>
    138.                                 <asp:TextBox ID="txtInsertBookCategory" required="required"  runat="server" Text='<%# Bind("BookCategory") %>'></asp:TextBox>
    139.                             </td>
    140.                         </tr>
    141.                         <tr>
    142.                             <td colspan="2">
    143.                                 <asp:Button ID="btnSave" runat="server" CommandName="Insert" Text="Save"/>
    144.                                 <asp:Button ID="btnCancelInsert" runat="server" CommandName="Cancel" Text="Cancel"   UseSubmitBehavior="false" CausesValidation="false" />
    145.                             </td>
    146.                         </tr>
    147.                     </table>
    148.                 </InsertItemTemplate>
    149.             </asp:FormView>

    Code Behind
    vb.net Code:
    1. Public Class FormViewDemo
    2.     Inherits System.Web.UI.Page
    3.  
    4.  
    5.     Dim _context As BooksEntities
    6.  
    7.  
    8.     Public Sub New()
    9.         _context = New BooksEntities()
    10.     End Sub
    11.  
    12.  
    13.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    14.         If Not Page.IsPostBack Then
    15.             BindFormView()
    16.         End If
    17.     End Sub
    18.  
    19.  
    20.     Private Sub BindFormView()
    21.         Dim bookRecords = _context.BookDetails.ToList()
    22.         FormViewBookDetails.DataSource = bookRecords
    23.         FormViewBookDetails.DataBind()
    24.     End Sub
    25.  
    26.  
    27.     Protected Sub FormViewBookDetails_ItemUpdating(sender As Object, e As FormViewUpdateEventArgs)
    28.         Dim id As Integer = Int32.Parse(FormViewBookDetails.DataKey(0).ToString())
    29.         Dim result = _context.BookDetails.Find(id)
    30.         If result IsNot Nothing Then
    31.             Dim item = New BookDetail()
    32.             item.BookSerialNo = id
    33.             item.BookISBN = DirectCast(FormViewBookDetails.FindControl("txtBookISBN"), TextBox).Text
    34.             item.BookTitle = DirectCast(FormViewBookDetails.FindControl("txtBookTitle"), TextBox).Text
    35.             item.BookAuthor = DirectCast(FormViewBookDetails.FindControl("txtBookAuthor"), TextBox).Text
    36.             item.BookPublisher = DirectCast(FormViewBookDetails.FindControl("txtBookPublisher"), TextBox).Text
    37.             item.BookCategory = DirectCast(FormViewBookDetails.FindControl("txtBookCategory"), TextBox).Text
    38.             _context.Entry(result).CurrentValues.SetValues(item)
    39.             _context.SaveChanges()
    40.             ResetBinding()
    41.         End If
    42.     End Sub
    43.  
    44.  
    45.     Protected Sub FormViewBookDetails_PageIndexChanging(sender As Object, e As FormViewPageEventArgs)
    46.         FormViewBookDetails.PageIndex = e.NewPageIndex
    47.         BindFormView()
    48.     End Sub
    49.  
    50.  
    51.     Protected Sub FormViewBookDetails_ItemCommand(sender As Object, e As FormViewCommandEventArgs)
    52.         If e.CommandName = "Cancel" Then
    53.             FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly)
    54.         ElseIf e.CommandName = "Edit" Then
    55.             FormViewBookDetails.ChangeMode(FormViewMode.Edit)
    56.         ElseIf e.CommandName = "New" Then
    57.             FormViewBookDetails.ChangeMode(FormViewMode.Insert)
    58.         ElseIf e.CommandName = "Delete" Then
    59.             FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly)
    60.             BindFormView()
    61.         End If
    62.     End Sub
    63.  
    64.  
    65.     Protected Sub FormViewBookDetails_ModeChanging(sender As Object, e As FormViewModeEventArgs)
    66.         If e.NewMode = FormViewMode.Insert Then
    67.             FormViewBookDetails.AllowPaging = False
    68.         ElseIf e.NewMode = FormViewMode.Edit Then
    69.             FormViewBookDetails.AllowPaging = False
    70.             BindFormView()
    71.         Else
    72.             FormViewBookDetails.AllowPaging = True
    73.             BindFormView()
    74.         End If
    75.     End Sub
    76.  
    77.  
    78.     Protected Sub FormViewBookDetails_ItemInserting(sender As Object, e As FormViewInsertEventArgs)
    79.         Dim item = New BookDetail()
    80.         item.BookISBN = DirectCast(FormViewBookDetails.FindControl("txtInsertBookISBN"), TextBox).Text
    81.         item.BookTitle = DirectCast(FormViewBookDetails.FindControl("txtInsertBookTitle"), TextBox).Text
    82.         item.BookAuthor = DirectCast(FormViewBookDetails.FindControl("txtInsertBookAuthor"), TextBox).Text
    83.         item.BookPublisher = DirectCast(FormViewBookDetails.FindControl("txtInsertBookPublisher"), TextBox).Text
    84.         item.BookCategory = DirectCast(FormViewBookDetails.FindControl("txtInsertBookCategory"), TextBox).Text
    85.         _context.BookDetails.Add(item)
    86.         _context.SaveChanges()
    87.         ResetBinding()
    88.     End Sub
    89.  
    90.  
    91.     Protected Sub FormViewBookDetails_ItemDeleting(sender As Object, e As FormViewDeleteEventArgs)
    92.         If FormViewBookDetails.DataKey(0) IsNot Nothing Then
    93.             Dim id = Int32.Parse(FormViewBookDetails.DataKey(0).ToString())
    94.             Dim itemDelete = _context.BookDetails.FirstOrDefault(Function(t) t.BookSerialNo = id)
    95.             If itemDelete IsNot Nothing Then
    96.                 _context.Entry(itemDelete).State = EntityState.Deleted
    97.                 _context.SaveChanges()
    98.                 ResetBinding()
    99.             End If
    100.         End If
    101.     End Sub
    102.  
    103.  
    104.     Private Sub ResetBinding()
    105.         FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly)
    106.         FormViewBookDetails.AllowPaging = True
    107.         BindFormView()
    108.     End Sub
    109. End Class

    Name:  FormViewDetails.jpg
Views: 2426
Size:  11.5 KB
    Last edited by KGComputers; Mar 16th, 2016 at 12:39 PM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width