I solved my problem by editing my Foreign Key Relationships of my tables, changing the INSERT and UPDATE Specification > Delete Rule to Cascade from No Action. The OnItemDeleting is not needed anymore, I will post this as an answer so if anyone also encounter this problem.
How to get the DeleteParameter value in to the Code behind?
-
29-06-2022 - |
Question
I have a ListView that enables deleting, I also use SQLDatasource as DataSource of the ListView, I have these 2 tables:
News table that contains news_id, title, etc.. and
Feedback table that contains feedback_id, comment, news_id(FK), etc...
I can delete a record that doesn't have comments but I get this error when I try to delete a News that has a comments, the comments is from the table 'Feedback' that has a foreign key 'news_id' from the table 'News', I know that to resolve this is I need to delete the records from the Feedback table first. How can I achieve it? I'm thinking of using OnItemDeleting Event in ListView and execute a query:
DELETE FROM Feedback WHERE news_id = @news_id
and then it can proceed to execute the DeleteCommand in the SQLDataSource that is:
DeleteCommand="DELETE FROM [News] WHERE [news_id] = @news_id"
but then I got this error:
Must declare the scalar variable "@news_id".
this is my DeleteParameter:
<DeleteParameters>
<asp:Parameter Name="news_id" Type="Int32" />
</DeleteParameters>
how can i get the news_id from the DeleteParameter? any answer is appreciated, thanks.
Solution