February 3, 2011

Delete data from DB Using LINQ to SQl

How to use Linq in ASP.NET App  refer my below post
http://www.dotnetissues.com/2011/02/select-query-linq-to-sql-example.html

To Delete data from DB , below is the code using LINQ



Default8.aspx

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


<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:GridView ID="GridView1" runat="server"/>
    </div>
    </form>
</body>
</html>


Default8.aspx.cs


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


public partial class Default8 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClasses2DataContext db = new DataClasses2DataContext();


//To delete the record
        var obj = from o in db.AdminInfos
                      where o.sLoginId=="TestLinq"
                      select o;


        db.AdminInfos.DeleteAllOnSubmit(obj);
        db.SubmitChanges();


//to get all the records from the table
        var results = from o in db.AdminInfos
                      select o;
        GridView1.DataSource = results;
        GridView1.DataBind();
    }
}

No comments:

Post a Comment