Retrieve ID of deleted Database row in C#
I have a FormView 开发者_运维知识库through which I set up the default new
, update
and delete
commands for communicating with an SQL Server database.
When the CREATE
command is executed the data is added to the database and some images are uploaded to a server and linked to the database.
Problem
When I press the Visual Studio's default FormView DELETE command I obviously only delete the selected row in the database and not the images on the server.
I marked the uploaded images with images ID + "name"
, so I could delete them if I could only retrieve the ID of the row being deleted by the DELETE command.
How do I do retrieve the ID of the deleted row in C#?
Probably the best solution would be to rework the design of your database a little bit and use a foreign key on your images. You could then set the images to be deleted by the database when the primary key (your data table) is deleted using a cascade.
Alternatively, assuming you cannot change your database schema, you can use the ItemCommand event from the FormView. This will let you intercept the commands as they're being processed; you can also handle the ItemDeleting or ItemDeleted events.
Handle the SqlDataSource's Deleting
event.
hm... i didn't know FormView.SelectedValue gets the id of the selected value from the formview, but as I tested now.. apparently it does...
精彩评论