Delete database record from tha database using .NET via stored procedure | YourSite

Delete database record from tha database using .NET via stored procedure

MS Dot NET 1046 views

File Name: ViewCabDetails.aspx

This this section we will cover how you can delete a database record from the database using .NET. Here we will use Stored Procedure for performing the database operation.



    <asp:GridView ID="GridView1Awesome" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="table table-hover" OnRowCommand="GridView1Awesome_RowCommand">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
          <asp:BoundField DataField="BookingReferenceNumber1" HeaderText="Booking ReferenceNumber " />
            <asp:BoundField DataField="PassengerName1" HeaderText="Passenger Name " />
            <asp:BoundField DataField="FromLocation1" HeaderText="From Location " />
            <asp:BoundField DataField="ToLocation1" HeaderText="To Location " />
            <asp:BoundField DataField="DepartureTime1" HeaderText="Departure Date Time " />
            <asp:BoundField DataField="DistanceinKm1" HeaderText="Distance in Km " />
            <asp:BoundField DataField="EstimatedAmount1" HeaderText="Estimated Amount " />
            <asp:BoundField DataField="CabType1" HeaderText="Cab Type " />  
            <asp:TemplateField HeaderText="Edit Detalis">
                <ItemTemplate >
                    <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("BookingReferenceNumber1") %>' runat="server" CommandName="EditEmployee">Edit</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            
    <asp:TemplateField HeaderText="Edit Detalis">
                <ItemTemplate >
                    <asp:LinkButton ID="LinkButton2" CommandArgument='<%# Eval("BookingReferenceNumber1") %>' runat="server" CommandName="DeleteEmployee">Delete</asp:LinkButton>
                </ItemTemplate>
          </asp:TemplateField>

       </Columns>
                <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>


File Name: ViewCabDetails.aspx.cs

CommandName="DeleteEmployee"


  protected void GridView1Awesome_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int BookingReferenceNumberTemp = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "DeleteEmployee")
            {                      
                bindControlsDelete(BookingReferenceNumberTemp); // THIS CODE FOR DELETE
            }
            else if (e.CommandName == "EditEmployee")
            {
                bindControls(BookingReferenceNumberTemp);
            }
        }


File Name: ViewCabDetails.aspx.cs



 private void bindControlsDelete(int BookingReferenceNumberTemp)
        {
            BookCabDBOperation cabBookDB = new BookCabDBOperation();
            Response.Write(String.Format("<script>alert('{0}')</script>", "Do you want to delete?"));            
            string message = cabBookDB.DeleteCabDetails(BookingReferenceNumberTemp);
            bindData();
            Response.Write(String.Format("<script>alert('{0}')</script>", message));
           

        }


File Name: BookCabDBOperation.cs

This is Data base Operation file



 public string DeleteCabDetails(int bookId)
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_delect_ByID", con);
            cmd.CommandType = CommandType.StoredProcedure; 
            cmd.Parameters.AddWithValue("@BookingReferenceNumber1", bookId);
            int rowsAffected = cmd.ExecuteNonQuery();
            string message;
            if (rowsAffected > 0)
                message = "Delete Successfully with Booking Id: "+ bookId;
            else
                message = "Some Error occured.";

            return message;

        }


SQL Procedure Name: sp_delect_ByID



CREATE PROCEDURE sp_delect_ByID  
@BookingReferenceNumber1 INT  
AS  
BEGIN  
DELETE FROM EtaxiSystem_1937935 WHERE BookingReferenceNumber = @BookingReferenceNumber1  
END

🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →