Click here to Skip to main content
12,698,476 members (23,355 online)
Click here to Skip to main content

Stats

79.1K views
3.4K downloads
93 bookmarked
Posted

CRUD Operations using LINQ Entities

, 11 Jul 2009 CPOL
CRUD operations using LINQ Entities
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";
        string strConnectionString = @"Data Source=mum-9018;Initial Catalog=TstServer;Integrated Security=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();
            objCustomerData.CustomerCode = txtCustomerCode.Text;
            objCustomerData.CustomerName = txtCustomerName.Text;
            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


You may also be interested in...

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