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

Rumman Ansari   2019-03-05   Student   MS dot NET > delete-operation-dot-net   839 Share

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