Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Entity Framework (EF) สอนเขียน LINQ to Entities บน .NET Application > ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework)



Clound SSD Virtual Server

ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework)

ASP.Net ตอนที่ 6 : Delete Data (LINQ, Entity Framework) ในตัวอย่างของตอนที่ 6 นี้จะเป็นการใช้ Entity Framework อ่านข้อมูลจาก Model Entities แสดงผลบน GridView และคลิกเลือกรายการลบน GridView เพื่อลบหรือ Delete ข้อมูล

Example : การใช้งาน LINQ to Entities แสดงผลบน GridView และเลือกรายการที่จะ Delete (ลบ)

Delete Data

Code Default.aspx (HTML)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="frmMain" runat="server">
        <asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="CUSTOMER_ID"
            OnRowDataBound="myGridView_RowDataBound" onRowDeleting="myGridView_RowDeleting" >
            <Columns>
                <asp:TemplateField HeaderText="Customer ID">
                    <ItemTemplate>
                        <asp:Label ID="lblCustomerID" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblEmail" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country Code">
                    <ItemTemplate>
                        <asp:Label ID="lblCountryCode" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Budget">
                    <ItemTemplate>
                        <asp:Label ID="lblBudget" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Used">
                    <ItemTemplate>
                        <asp:Label ID="lblUsed" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </form>
</body>
</html>

Code Default.aspx.cs (C#)
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            // Create new entities Object
            using (var db = new myDatabaseEntities())
            {
                // Get data from CUSTOMER
                var ds = (from c in db.CUSTOMER
                          select new
                          {
                              c.CUSTOMER_ID,
                              c.NAME,
                              c.EMAIL,
                              c.COUNTRY_CODE,
                              c.BUDGET,
                              c.USED
                          }).ToList();

                // Assign to GridView
                if (ds.Count > 0)
                {
                    this.myGridView.DataSource = ds;
                    this.myGridView.DataBind();
                }
            }
        }

        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID") != null)
            {
                //*** CustomerID ***//
                Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
                if (lblCustomerID != null)
                {
                    lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID").ToString();
                }

                //*** Name ***//
                Label lblName = (Label)(e.Row.FindControl("lblName"));
                if (lblName != null)
                {
                    lblName.Text = DataBinder.Eval(e.Row.DataItem, "NAME").ToString();
                }

                //*** Email ***//
                Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
                if (lblEmail != null)
                {
                    lblEmail.Text = DataBinder.Eval(e.Row.DataItem, "EMAIL").ToString();
                }

                //*** CountryCode ***//
                Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
                if (lblCountryCode != null)
                {
                    lblCountryCode.Text = DataBinder.Eval(e.Row.DataItem, "COUNTRY_CODE").ToString();
                }

                //*** Budget ***//
                Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
                if (lblBudget != null)
                {
                    lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "BUDGET").ToString();
                }

                //*** Used ***//
                Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
                if (lblUsed != null)
                {
                    lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "USED").ToString();
                }

                //*** Delete ***//
                LinkButton lnkDelete = (LinkButton)(e.Row.FindControl("lnkDelete"));
                if (lnkDelete != null)
                {
                    lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record " + lblCustomerID.Text + " ?');");
                }

            }

        }

        protected void myGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get Customer ID from DataKey
            string strCustomerID = this.myGridView.DataKeys[e.RowIndex].Value.ToString();

            // Create new entities from Entities
            using (var db = new myDatabaseEntities())
            {
                // Delete CUSTOMER
                var del = (from c in db.CUSTOMER
                           where c.CUSTOMER_ID == strCustomerID
                           select c).FirstOrDefault();
                if (del != null)
                {
                    db.CUSTOMER.Remove(del);
                }

                db.SaveChanges();
                BindData();
                ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('" + string.Format("Delete {0} Successfully.", strCustomerID) + "');</script>");
            }
        }

    }

Screenshot

Delete Data

เลือกรายการที่ต้องการจะลบ และคลิกที Delete ซึ่งระบบจะแสดง Dialog ให้ Confirm การ Delete ข้อมูล

Delete Data

หลังจากที่ Delete เรียบร้อยแล้วจะทำการ Bind ข้อมูลใน GridView ใหม่








Code Default.aspx.vb (VB.Net)
Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub

    Protected Sub BindData()
        ' Create new entities Object
        Using db = New myDatabaseEntities()

            ' Get data from CUSTOMER
            Dim ds = (From c In db.CUSTOMER
                      Select New With { _
                        c.CUSTOMER_ID, _
                        c.NAME, _
                        c.EMAIL, _
                        c.COUNTRY_CODE, _
                        c.BUDGET, _
                        c.USED _
                    }).ToList()

            ' Assign to GridView
            If ds.Count > 0 Then
                Me.myGridView.DataSource = ds
                Me.myGridView.DataBind()
            End If
        End Using

    End Sub

    Protected Sub myGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles myGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow And Not IsNothing(DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID")) Then
            '*** CustomerID ***'
            Dim lblCustomerID As Label = DirectCast(e.Row.FindControl("lblCustomerID"), Label)
            If Not IsNothing(lblCustomerID) Then
                lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "CUSTOMER_ID").ToString()
            End If

            '*** Name ***'
            Dim lblName As Label = DirectCast(e.Row.FindControl("lblName"), Label)
            If Not IsNothing(lblName) Then
                lblName.Text = DataBinder.Eval(e.Row.DataItem, "NAME").ToString()
            End If

            '*** Email ***'
            Dim lblEmail As Label = DirectCast(e.Row.FindControl("lblEmail"), Label)
            If Not IsNothing(lblEmail) Then
                lblEmail.Text = DataBinder.Eval(e.Row.DataItem, "EMAIL").ToString()
            End If

            '*** CountryCode ***'
            Dim lblCountryCode As Label = DirectCast(e.Row.FindControl("lblCountryCode"), Label)
            If Not IsNothing(lblCountryCode) Then
                lblCountryCode.Text = DataBinder.Eval(e.Row.DataItem, "COUNTRY_CODE").ToString()
            End If

            '*** Budget ***'
            Dim lblBudget As Label = DirectCast(e.Row.FindControl("lblBudget"), Label)
            If Not IsNothing(lblBudget) Then
                lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "BUDGET").ToString()
            End If

            '*** Used ***'
            Dim lblUsed As Label = DirectCast(e.Row.FindControl("lblUsed"), Label)
            If Not IsNothing(lblUsed) Then
                lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "USED").ToString()
            End If

            '*** Delete ***'
            Dim lnkDelete As LinkButton = DirectCast(e.Row.FindControl("lnkDelete"), LinkButton)
            If Not IsNothing(lnkDelete) Then
                lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record " + lblCustomerID.Text + " ?');")
            End If
        End If
    End Sub

    Protected Sub myGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles myGridView.RowDeleting
        ' Get Customer ID from DataKey
        Dim strCustomerID As String = Me.myGridView.DataKeys(e.RowIndex).Value.ToString()

        ' Create new entities from Entities
        Using db = New myDatabaseEntities()
            ' Delete CUSTOMER
            Dim del = (From c In db.CUSTOMER
                       Where c.CUSTOMER_ID = strCustomerID
                       Select c).FirstOrDefault()
            If Not IsNothing(del) Then
                db.CUSTOMER.Remove(del)
            End If

            db.SaveChanges()
            BindData()
            ClientScript.RegisterStartupScript(Me.GetType, "PopupScript", "<script>alert('" + String.Format("Delete {0} Successfully.", strCustomerID) + "');</script>")
        End Using

    End Sub
End Class


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-10-02 21:22:04 / 2017-03-24 22:59:00
  Download : No files
 Sponsored Links / Related

 
รู้จัก Entity Framework (EF) คืออะไร ใช้ทำอะไร และมีประโยชน์อย่างไรบน .Net Framework
Rating :

 
สร้าง Entity Framework เพื่อติดต่อกับ Database การสร้าง Model Entities บน Visual Studio
Rating :

 
List(T) คืออะไร Generic Class ของ List ใช้งานกับ Entity Framework และ LINQ to Entities
Rating :

 
เรียกใช้งาน Entity Framework การ Select ข้อมูลจาก Entities แสดงผลบน DataSource Control
Rating :

 
Loop and Get Data (LINQ, Entity Framework)
Rating :

 
WHERE Clause (LINQ, Entity Framework)
Rating :

 
Entity & Relation (Foreign Key , CasCade , Delete , Update) (LINQ, Entity Framework)
Rating :

 
WHERE IN / NOT IN (LINQ, Entity Framework)
Rating :

 
WHERE LIKE / NOT LIKE (LINQ, Entity Framework)
Rating :

 
ORDER BY and Sorting Data (ASC, DESC) (LINQ, Entity Framework)
Rating :

 
WHERE BETWEEN (LINQ, Entity Framework)
Rating :

 
Compare DateTime (LINQ, Entity Framework)
Rating :

 
Join Clause (LINQ, Entity Framework)
Rating :

 
Left Join (LINQ, Entity Framework)
Rating :

 
Right Join (LINQ, Entity Framework)
Rating :

 
Union (LINQ, Entity Framework)
Rating :

 
VIEW Table (LINQ, Entity Framework)
Rating :

 
Add Entity : Insert Rows Into Database (LINQ, Entity Framework)
Rating :

 
Update Entity : Update Rows in Database (LINQ, Entity Framework)
Rating :

 
Delete Entity : Delete Rows in Database (LINQ, Entity Framework)
Rating :

 
Entity Framework and Try Catch Exception (LINQ, Entity Framework)
Rating :

 
Entity Framework and Transaction Scope (LINQ, Entity Framework)
Rating :

 
Get Connection String & Connection State (LINQ, Entity Framework)
Rating :

 
Select Entity to List<Object> (LINQ, Entity Framework)
Rating :

 
Entity Execute SQL Query : INSERT/UPDATE/DELETE (LINQ, Entity Framework)
Rating :

 
Entity Select SQL (Statement) Query (LINQ, Entity Framework)
Rating :

 
Entity and Stored Procedure (LINQ, Entity Framework)
Rating :

 
เทคนิคการใช้ IntelliTrace ในการ Debug ตรวจสอบ Performance ของโปรแกรม
Rating :

 
Windows Form ตอนที่ 1 : DataGridView List Show Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 2 : DataGridView Search Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 3 : DataGridView Display Master-Detail (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 4 : Add Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 5 : Update Data (LINQ, Entity Framework)
Rating :

 
Windows Form ตอนที่ 6 : Delete Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 1 : GridView List Show Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 2 : GridView Search Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 3 : Display Master-Detail (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 4 : Add Data (LINQ, Entity Framework)
Rating :

 
ASP.Net ตอนที่ 5 : Update Data (LINQ, Entity Framework)
Rating :

 
การใช้งาน Entity Framework กับ MySQL Database (LINQ to Entities - MySQL Database)
Rating :

 
การใช้งาน Entity Framework กับ Oracle Database (LINQ to Entities - Oracle Database)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่