Showing posts with label Remove Record Hard Delete using Entity Framework Asp.Net WebForm. Show all posts
Showing posts with label Remove Record Hard Delete using Entity Framework Asp.Net WebForm. Show all posts

Tuesday, 28 February 2023

05 Delete Record using Entity Framework Asp Net WebForm C# | Remove Record Hard Delete using Entity Framework Asp.Net WebForm


05 Delete Record using Entity Framework Asp Net WebForm C#, Remove Record Hard Delete using Entity Framework Asp.Net WebForm





Delete Page ASPX Code   (Delete-Friend.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="delete-friend.aspx.cs" Inherits="ManojKallaTutorial.delete_friend" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <table>
            <tr>
                <td>
                    Fullname
                </td>
                <td>
                    <asp:TextBox ID="txtFullName" runat="server" ReadOnly="true"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    City
                </td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server" ReadOnly="true"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    Mobile
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server" ReadOnly="true"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Delete" OnClick="btnSubmit_Click" />
                </td>
                <td>
                    <a href="Friend.aspx">Back</a>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



Delete Page C# Code (Delete-Friend.aspx.cs):


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

namespace ManojKallaTutorial
{
    public partial class delete_friend : System.Web.UI.Page
    {
        dbMbkTestEntities db = new dbMbkTestEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                {
                    Int32 FIdInt;
                    string FID = Convert.ToString(Request.QueryString["fid"]);
                    if (!string.IsNullOrEmpty(FID))
                    {
                        FIdInt = Convert.ToInt32(FID);
                        var friendDet = (from a in db.tblFriends where a.FriendID == FIdInt select a).FirstOrDefault();
                        if (friendDet != null)
                        {
                            txtFullName.Text = friendDet.FriendName;
                            txtCity.Text = friendDet.City;
                            txtMobile.Text = friendDet.Mobile;
                        }
                    }
                    else
                    {
                        Response.Redirect("friend.aspx?msg=invalidfriend");
                    }

                }
                else
                {
                    Response.Redirect("friend.aspx?msg=NoFriendFoundOrInvalidFriend");
                }

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (Request.QueryString.Count > 0)
            {
                Int32 FIdInt;
                string FID = Convert.ToString(Request.QueryString["fid"]);
                if (!string.IsNullOrEmpty(FID))
                {
                    FIdInt = Convert.ToInt32(FID);
                    var friendDet = (from a in db.tblFriends where a.FriendID == FIdInt select a).FirstOrDefault();
                    if (friendDet != null)
                    {
                        db.tblFriends.Remove(friendDet);
                        db.SaveChanges();
                        Response.Redirect("friend.aspx?msg=successfullydeleted");
                    }
                }
                else
                {
                    Response.Redirect("friend.aspx?msg=invalidfriend");
                }

            }
            else
            {
                Response.Redirect("friend.aspx?msg=NoFriendFoundOrInvalidFriend");
            }


        }
    }
}