ASP.NET
  Home arrow ASP.NET arrow Page 3 - Introduction to the ADO.NET Entity Framewo...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Introduction to the ADO.NET Entity Framework using ASP.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-04-09

    Table of Contents:
  • Introduction to the ADO.NET Entity Framework using ASP.NET
  • CRUD operations using ADO.NET Entity Framework: Screen Design
  • CRUD operations using ADO.NET Entity Framework: Source Code
  • Digging a bit more into the ADO.NET Entity Framework
  • What are Entity SQL queries?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to the ADO.NET Entity Framework using ASP.NET - CRUD operations using ADO.NET Entity Framework: Source Code


    (Page 3 of 5 )

     

    This section is a continuation from the previous section. Modify the code behind so that it looks like this: 

    Public Partial Class CRUD

    Inherits System.Web.UI.Page

     

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    Dim ctxt As New NorthwindEntities

    Dim oEmp As Emp = (From o In ctxt.Emp.Include("Dept") _

    Where o.Empno = Me.txtEmpno.Text _

    Select o).FirstOrDefault

    If oEmp Is Nothing Then

    btnClear_Click(Nothing, Nothing)

    Me.lblMsg.Text = "Employee not found."

    Exit Sub

    End If

     

    Me.txtEname.Text = oEmp.Ename

    Me.txtSal.Text = oEmp.Sal

    Me.txtDeptno.Text = oEmp.Dept.Deptno

     

    End Sub

     

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click

    Me.txtEmpno.Text = ""

    Me.txtEname.Text = ""

    Me.txtSal.Text = ""

    Me.txtDeptno.Text = ""

    Me.lblMsg.Text = ""

    End Sub

     

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click

     

    Try

    Dim oEmp As New Emp With {.Empno = Me.txtEmpno.Text, .Ename = Me.txtEname.Text, .Sal = Me.txtSal.Text}

    Dim oDeptKey As New EntityKey("NorthwindEntities.Dept", "Deptno", Integer.Parse(Me.txtDeptno.Text))

    oEmp.DeptReference.EntityKey = oDeptKey

     

    Dim ctxt As New NorthwindEntities

    ctxt.AddToEmp(oEmp)

    ctxt.AcceptAllChanges()

    ctxt.SaveChanges()

     

    Me.lblMsg.Text = "Added Successfully!"

    Catch ex As Exception

    If ex.Message.ToLower.Contains("innerexception") Then

    Me.lblMsg.Text = ex.InnerException.Message

    Else

    Me.lblMsg.Text = ex.Message

    End If

    End Try

     

    End Sub

     

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click

    Dim ctxt As New NorthwindEntities

    Dim oEmp As Emp = ctxt.Emp.Where(Function(p) p.Empno = Me.txtEmpno.Text).FirstOrDefault

    If oEmp Is Nothing Then

    Me.lblMsg.Text = "Employee not found to update"

    Exit Sub

    End If

     

    Dim oDept As Dept = ctxt.Dept.Where(Function(p) p.Deptno = Me.txtDeptno.Text).FirstOrDefault

    If oDept Is Nothing Then

    Me.lblMsg.Text = "Invalid Deptno"

    Exit Sub

    End If

     

    oEmp.Ename = Me.txtEname.Text

    oEmp.Sal = Me.txtSal.Text

    oEmp.Dept = oDept

    ctxt.SaveChanges()

     

    Me.lblMsg.Text = "Updated Successfully!"

    End Sub

     

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click

    Dim ctxt As New NorthwindEntities

    Dim oEmp As Emp = ctxt.Emp.Where(Function(p) p.Empno = Me.txtEmpno.Text).FirstOrDefault

    If oEmp Is Nothing Then

    Me.lblMsg.Text = "Employee not found to delete"

    Exit Sub

    End If

    ctxt.DeleteObject(oEmp)

    ctxt.SaveChanges()

    btnClear_Click(Nothing, Nothing)

    Me.lblMsg.Text = "Deleted Successfully!"

    End Sub

     

    End Class

     

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · Hai,This is my first article on dealing with ADO.NET Entity framework with...
       · Thanks man it reduced me lot of lines of code.
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek