News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. ***
  May 30 2015 9:54PM     Micheal
  2 Comments    2877 Views  
Here Micheal provided a short tutorial with example, Select, Insert, Update and Delete in LINQ C#.
Create a Employee Table in the Database

CREATE TABLE [dbo].[Employee]
(
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [EmployeeName] [nvarchar](50) NOT NULL,
        [EmployeeDesc] [nchar](10) NOT NULL,
        [EmployeeAge] [int] NOT NULL,
        [EmployeeAddress] [nvarchar](max) NOT NULL,
        [EmployeeExperience] [int] NOT NULL
)

Steps to Create .dbml (LINQ to SQL Classes)
Step 1: Open Visual Studio and Create ASP.Net Web Application.
Step 2: Right click the solution and Select Add New Item.. select Linq to sql classes option from the list
Step 3: The image given belows shows to Select Linq to sql classes in Add New Item Popup Window
Step 4: After clicking .dbml file, Now we should drag all the tables from the Database (Server Explorer).
Step 5: After placing tables in DataContext file, you dbml should look like this.
Webconfig
For .dbml files the database connection string is defined in the web.config file as:

<connectionStrings>
<add name="DevelopmentConnectionString" connectionString="Data Source=XXX;Initial Catalog=DemoDB;User ID=sa;
Password=*******"
 providerName="System.Data.SqlClient" />

 </connectionStrings>

Select Operation
Default.aspx
HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2><b><div>The Developer Tips</div></b></h2>
    
    <h3><b>Gridview with LINQ</b></h3>

    <asp:GridView ID="gridviewEmp" runat="server">
    </asp:GridView>
    </div>
    </form>
</body>
</html>

C#

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindGridView()
        {
            using (DataContext objDataContext = new DataContext())
            {
                var result = from c in objDataContext.Employees
                             where c.EmployeeAge >= 20
                             select c;                

                List<Employee> lstEmployee = result.ToList();
                gridviewEmp.DataSource = lstEmployee;
                gridviewEmp.DataBind();
            };
        }
    }

Insert Operation

        private void AddNewEmployee()
        {
            using (DataContext objDataContext = new DataContext())
            {
                Employee objEmp = new Employee();
                // fields to be insert
                objEmp.EmployeeName = "";
                objEmp.EmployeeAge = 25;
                objEmp.EmployeeDesc = "";
                objEmp.EmployeeAddress = "";
                objEmp.EmployeeExperience = 8;
                objDataContext.Employees.InsertOnSubmit(objEmp);
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };
        }

Update Operation

        private void UpdateEmployee()
        {
            using (DataContext objDataContext = new DataContext())
            {
                // Get single employee to update
                Employee objEmp = objDataContext.Employees.Single(emp => emp.EmployeeName == "john");
                //Field which will be update
                objEmp.EmployeeExperience = 10;
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };
        }

Delete Operation

        private void DeleteEmployee()
        {
            using (DataContext objDataContext = new DataContext())
            {
                // Get single employee to update
                Employee objEmp = objDataContext.Employees.Single(emp => emp.EmployeeName == "john");              
                objDataContext.Employees.DeleteOnSubmit(objEmp);
                // executes the commands to implement the changes to the database
                objDataContext.SubmitChanges();
            };
        }

Result (Select Operation)
I hope this page will helps to learn Select, Insert, Update and Delete operations in LINQ C#. Thanks.
BackToTop
Comments


AnonymousUser
Nov 17 2016 7:06AM
Its wont work for me! Where objDataContext.Employees.InsertOnSubmit(objEmp); come from?
Arun Kumar
Nov 19 2016 12:56AM
Please refer Step 3, he was included LINQ to SQL Classess and renamed as DataContext.dbml

Then follow Step4 and Step5

In AddNewEmployee() method he was created a object for DataContext, ie objDataContext. From the object objDataContext you can have objDataContext.Employees.InsertOnSubmit(objEmp);"

Please see this link for more explanation about Getting Started with LINQ C#

I hope it helps you. Thanks

 
Search
1&1 Hosting
Recent Posts
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2016 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2016 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2016 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2016 10:20AM Posted By Micheal
Read excel file and convert to List in C#
Feb 25 2016 6:26AM Posted By Michael
Encryption and Decryption for Password in C#
Feb 17 2016 3:52AM Posted By Michael
How to create Chart in ASP.Net C#
Feb 16 2016 8:03AM Posted By Michael Ryan
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: