Share Blog

Saturday, May 24, 2014

LINQ TO SQL insert,Update ,Delete,Show And Search Using Ado.net

Language-INtegrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages

LINQ to SQL

LINQ to SQL translates our actions to SQL and submits the changes to the database. Here we will perform Select, Insert, Update and Delete operations on a LINQ_Table table


Step 1: Create a LINQ_Table Table in the database
create table LINQ_Table(id primary key,name varchar(50),address varchar(100))

Step 2: Create a ContextData file using the Object Relational Designer:

Create a new item, select the LINQ to SQL classes (as shown in the following figure) and name it DataClasses.dbml.



After clicking the Add button the ContextData file is created. Now we should drag all the tables onto the left-hand side of the designer and save (as shown in the following figure). This will create all the mappings and settings for each table and their entities.



NOTE:Drag on web page And Save The Table 


-------------------LINQ_Example.aspx.cs FOR CODE---------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LINQ_Example.aspx.cs" Inherits="LINQ_Example" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style3
        {
            width: 128px;
        }
        .style4
        {
            width: 81px;
        }
        .style5
        {
            width: 71px;
        }
        .style6
        {
            width: 56px;
        }
        .style7
        {
            width: 112px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style7">
                    <asp:Label ID="Label1" runat="server" Text="Employee ID"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
                    &nbsp;</td>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Label ID="Label2" runat="server" Text="Employee Name"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
                    &nbsp;</td>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Label ID="Label3" runat="server" Text="Address"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
                <td class="style4">
                    &nbsp;</td>
                <td class="style5">
                    &nbsp;</td>
                <td class="style6">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="INSERT" />
                </td>
                <td class="style3">
                    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="UPDATE" />
                </td>
                <td class="style4">
                    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="DELETE" />
                </td>
                <td class="style5">
                    <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="SHOW" />
                </td>
                <td class="style6">
                    <asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="SEARCH" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    <asp:GridView ID="GridView1" runat="server" BackColor="White"
        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>
    </form>
</body>
</html>




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

public partial class LINQ_Example : System.Web.UI.Page
{
    //create DataContext object
    DataClassesDataContext dc = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Data maping object to our database
        var v = from i in dc.LINQ_Tables select i;
        LINQ_Table item = new LINQ_Table();
            item.id = Convert.ToInt32(TextBox1.Text);
            item.name = TextBox2.Text;
            item.address = TextBox3.Text;
            //Adds an entity in a pending insert state to this System.Data.Linq.Table and parameter is the entity which to be added
        dc.LINQ_Tables.InsertOnSubmit(item);
        // executes the appropriate commands to implement the changes to the database
        dc.SubmitChanges();
        Page.RegisterStartupScript("open","<script>alert('Data Save Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int n = Convert.ToInt32(TextBox1.Text);
        //Get Single course which need to update
        var v = from i in dc.LINQ_Tables where i.id == n select i;
        //Field which will be update
        foreach(var item in v)
        {
            item.name = TextBox2.Text;
            item.address = TextBox3.Text;
        }
        // executes the appropriate commands to implement the changes to the database
        dc.SubmitChanges();
        Page.RegisterStartupScript("open", "<script>alert('Data Updated Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int n = Convert.ToInt32(TextBox1.Text);
        //Get Single course which need to Delete
        var v = from i in dc.LINQ_Tables where i.id == n select i;

        foreach(var item in v)
        {
            //Puts an entity from this table into a pending delete state and parameter is the entity which to be deleted.
            dc.LINQ_Tables.DeleteOnSubmit(item);
        }
        // executes the appropriate commands to implement the changes to the database
        dc.SubmitChanges();
        Page.RegisterStartupScript("open", "<script>alert('Data Deleted Successfully')</script>'");
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        //Data maping object to our database
        var v=from i in dc.LINQ_Tables  select i;
        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        int n = Convert.ToInt32(TextBox1.Text);
        //ckeck Data in Table From Database And Search Information
        var v = from i in dc.LINQ_Tables where i.id == n select i;
        //Data bind And Show Data in GridView
        GridView1.DataSource = v;
        GridView1.DataBind();
    }

}

RESULT:

------SHOW RECORD IN TABLE----


------INSERT RECORD IN TABLE----
INSERT RECORD ID=6,name=AVDESH,address=MP

------UPDATE RECORD IN TABLE----
UPDATE ID =5,name=MANSHI,address=GHAZIABAD 



------DELETE RECORD IN TABLE----

DELETE ID=1





No comments:

Post a Comment