LINQ to SQL Crude operation in ASP.Net



LinQ Crude operation in asp.net



Introduction :   LINQ means (Language Integrated Query)  Using Linq you can query any datasource without knowing the database query knowledge  like sql server,My Sql,xml datasource etc. and also it easy to query to database for manipulating the data to the database.

Here this tutorial explain in asp.net(c#) with how to perform insert ,update ,delete,edit opearation, using the linq

Tools :   Visual Studio 2010 ,Sql Server 2008R2

Step 1:   First Create Database

Step 2:   Now add  Linq to DBML Class and drag the database table  as Following





Step3 :  Now  add  the asp.net webform as below source


Linq Crude



Step 4:    Now Perform the Insert update,delete and edit operation in following code, first you can add  three Textbox and one GridView then bind it as following code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LinqDemo
{
    public partial class user : System.Web.UI.Page
    {

        //  here dataDataContext   is  is context class created when .dbml class is created

        dataDataContext DB = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            var sel = from tb1 in DB.user_masters
                      select new
                      {
                          tb1.name,
                          tb1.adress,
                          tb1.contact
                      };
            GridView1.DataSource = sel;
            GridView1.DataBind();
        }    
        protected void btn_insert_Click(object sender, EventArgs e)  //Insert Operation
        {
            user_master u1 = new user_master();
            u1.name = TextBox1.Text;
            u1.adress = TextBox2.Text;
            u1.contact = TextBox3.Text;

            DB.user_masters.InsertOnSubmit(u1);
            DB.SubmitChanges();
            Response.Write("inserted Data......");          
            GridView1.DataBind();
        }
        protected void btn_edit_Click(object sender, EventArgs e)
        {
            var edi = (from tb1 in DB.user_masters
                       where tb1.name == Convert.ToString(TextBox1.Text)
                       select tb1).Single();
                        {
                            TextBox2.Text = edi.adress;
                            TextBox3.Text = edi.contact;
                        };
        }
        protected void Button1_Click(object sender, EventArgs e)   //update
        {
            var up = (from tb1 in DB.user_masters
                      where tb1.name == Convert.ToString(TextBox1.Text)
                      select tb1).First();
            {
                up.adress = TextBox2.Text;
                up.contact = TextBox3.Text;
            };
            DB.SubmitChanges();

            Response.Write("data updated");
        }

        protected void Button2_Click(object sender, EventArgs e)   // Delete Operation
        {
            var dl = (from tb1 in DB.user_masters
                      where tb1.name == Convert.ToString(TextBox1.Text)
                      select tb1).First();
            {
                dl.adress = TextBox2.Text;
                dl.contact = TextBox3.Text;

             
            };
            DB.user_masters.DeleteOnSubmit(dl);

            DB.SubmitChanges();

            Response.Write("Data Deleted");
        }
    }
}

Now run the program and see the O/P and if you have any Query Reguirding Linq then feel free to comment below.
Previous
Next Post »