Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CRUD Operations using LINQ Entities

, 11 Jul 2009 CPOL
CRUD operations using LINQ Entities
LINQCrudWithEntities.zip
LINQCrudWithEntities
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQCrudWithEntities.pdb
LINQtoSQLStoredProc.dll
LINQtoSQLStoredProc.pdb
LINQCrudWithEntities.csproj.user
Properties
LINQCrudWithEntities.suo
LINQCrudWithEntities
App_Data
Sales.mdf
Sales_log.LDF
bin
LINQCrudWithEntities.dll
LINQCrudWithEntities.pdb
LINQtoSQLStoredProc.dll
LINQtoSQLStoredProc.pdb
LINQCrudWithEntities.csproj.user
Properties
LINQCrudWithEntities.suo
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;
namespace LINQCrudWithEntities
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Sales.mdf;Integrated Security=True;User Instance=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            
            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntity>()
                          where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
                          select objCustomer;
            
            clsCustomerEntity objCustomerData = (clsCustomerEntity)MyQuery.First<clsCustomerEntity>();
            txtCustomerCode.Text = objCustomerData.CustomerCode;
            txtCustomerName.Text = objCustomerData.CustomerName;
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);

            clsCustomerEntity objCustomerData = new clsCustomerEntity();
            
            
            objContext.GetTable<clsCustomerEntity>().InsertOnSubmit(objCustomerData);
            objContext.SubmitChanges();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);

            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntity>()
                          where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
                          select objCustomer;

            clsCustomerEntity objCustomerData = (clsCustomerEntity)MyQuery.First<clsCustomerEntity>();
            objCustomerData.CustomerCode = txtCustomerCode.Text;
            objCustomerData.CustomerName = txtCustomerName.Text;
            objContext.SubmitChanges();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);

            var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntity>()
                          where objCustomer.CustomerId == Convert.ToInt16(txtCustomerId.Text)
                          select objCustomer;
            clsCustomerEntity objCustomerData = (clsCustomerEntity)MyQuery.First<clsCustomerEntity>();
            objContext.GetTable<clsCustomerEntity>().DeleteOnSubmit(objCustomerData);
            objContext.SubmitChanges();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 11 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid