0

I am trying to delete a gridview row in the back end. When I click delete it deletes from the gridview, however it does not delete from the database. Can anyone see why this may be? My code is as follows:

protected void GVMyBookings_DeleteBooking(object sender, GridViewDeleteEventArgs e)
{
     string connstring = ConfigurationManager.ConnectionStrings["BookingConn"].ToString();
     SqlConnection MyConnection = new SqlConnection(connstring);

     MyConnection.Open();

     SqlDataSource SDSBooking= new SqlDataSource();
     SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK";
     SDSBooking.DeleteParameters.Add("@BookingID_PK", GVMyBookings.Rows[e.RowIndex].Cells[0].ToString());
     SDSBooking.ConnectionString = connstring;

     GVMyBookings.DataSource = SDSBooking;
     GVMyBookings.DataBind();
     MyConnection.Close();
}

The gridview is:

<asp:GridView ID="GVMyBookings" runat="server" GridLines="Vertical" AllowSorting="True"
    AutoGenerateColumns="False" AutoGenerateDeleteButton="true" 
    OnRowDeleting="GVMyBookings_DeleteBooking" EmptyDataText="You have no upcoming bookings" >
    <RowStyle BackColor="#e5ecbf" />
    <Columns>
        <asp:BoundField DataField="BookingID_PK"  />
        <asp:BoundField DataField="BookingDate" HeaderText="Booking Date" 
            SortExpression="BookingDate" DataFormatString="{0:d}" />
        <asp:BoundField DataField="RoomName" HeaderText="Room Name" 
            SortExpression="RoomName" />
        <asp:BoundField DataField="StartTime" HeaderText="Start Time" 
            SortExpression="StartTime"/>
        <asp:BoundField DataField="EndTime" HeaderText="End Time" 
            SortExpression="EndTime" />
        <asp:BoundField DataField="StaffUID" HeaderText="StaffUID" 
            SortExpression="StaffUID" Visible="false" />
    </Columns>
    <HeaderStyle BackColor="#264409" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

2 Answers 2

2

Seems that your query is incorrect

DELETE * FROM Tbl_Booking WHERE ID=@BookingID_PK

Use this instead

DELETE FROM Tbl_Booking WHERE ID=@BookingID_PK

Remove the @ symbole from this line

SDSBooking.DeleteParameters.Add("BookingID_PK",...); 

Then call Delete explicitly against the dataSource

... 
SDSStudents.ConnectionString = connstring;
SDSStudents.Delete();
GridView1.DataSource = SDSStudents;  
...
Sign up to request clarification or add additional context in comments.

4 Comments

An error occured on the SDSBooking.Delete() saying: Conversion failed when converting the nvarchar value 'System.Web.UI.WebControls.GridViewRow' to data type int
@Claire Try to replace this GVMyBookings.Rows[e.RowIndex].Cells[0].ToString() by GridView1.Rows[e.RowIndex].Cells[0].Text
No error, it deletes from the gridview, but then when I refresh the page it is there again - so it still isn't updating the database. Is this all in the correct order?: SqlDataSource SDSBooking = new SqlDataSource(); SDSBooking.ConnectionString = connstring; SDSBooking.Delete(); SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK"; SDSBooking.DeleteParameters.Add("BookingID_PK", GVMyBookings.Rows[e.RowIndex].Cells[0].Text); GVMyBookings.DataSource = SDSBooking; GVMyBookings.DataBind(); MyConnection.Close();
@Claire SDSBooking.Delete(); and SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK"; are reversed
0

Your code sequence to delete the record from database should be like this.

//Declaring the datasource.
SqlDataSource SDSBooking= new SqlDataSource();

//Providing the delete command.
SDSBooking.DeleteCommand = "DELETE FROM Tbl_Booking WHERE BookingID_PK = @BookingID_PK";

//Adding the parameter for deleting the record.
SDSBooking.DeleteParameters.Add("BookingID_PK", GVMyBookings.Rows[e.RowIndex].Cells[1].Text);

//Providing the connection string.
SDSBooking.ConnectionString = connstring;

//Executing the delete method of the SqlDataSouce. 
//It is this line that will actually delete your record.
SDSBooking.Delete();

After this you can assign this datasource to you gridview.

Note : For GVMyBookings.Rows[e.RowIndex].Cells[1].Text, you need to make sure, that your databound field is at index 1 in the TableCellCollection.

You might need to change, if you are generating some other columns before it.

As because in your case the delete button would be generated before your databound field. So the delete button would be at index 0 & your databound field *BookingID_PK* would be at index 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.