Reputation: 63
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>
Upvotes: 0
Views: 595
Reputation: 3611
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.
Upvotes: 0
Reputation: 2104
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;
...
Upvotes: 2