Click here to Skip to main content
13,089,433 members (60,653 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i have two tables stored images path, and images are stored in a folder,

pid int
pname nvarchar(50)
pimages nvarchar(max)
fid int , primary key
fname nvarchar(50)
fimage nvarchar(max)
pid int
each image from the first table related with the images from the second table
( one to many relationship )

I want when delete the image from the first table, delete with its images of the second table and the images also delete from the folder .

any help.... ^_^

Posted 23-Jan-13 4:33am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Delete from table1,table2 where pid= YourID

I am not sure but It should work.

If you are deleting it from C# then pass your ID when user click on delete button. and Use that ID in above SQL Query.
leana88 23-Jan-13 10:52am
ok , and the images How to delete it from folder ?
Member 9581488 23-Jan-13 10:55am
Please refer below link:-
You can pass your PATH(where your file is saved) to find if the file exists, if it does then Delete it!!!!!
leana88 23-Jan-13 11:01am
Well, i will try with this link.
Thank you ^_^
Member 9581488 23-Jan-13 11:07am
let me know if you cant do it.
I will def help you out.
leana88 24-Jan-13 11:26am
Hi ^_^
I could not do it > :(
because i have relationship between tables ,
Do you have an example on this topic ?
Member 9581488 24-Jan-13 11:33am
For deleting in database or in folder?
leana88 24-Jan-13 11:45am
only delete from folder... because this is my problem.
Member 9581488 24-Jan-13 11:54am
You have to find out file names that need to be deleted using select statement (for pImages & fImages) and pass your ID using join . After getting result in dataset or datatable, Loop through each row and find if that file exists in your folder if yes then delete it one by one.
let me know if you still find it difficult.
leana88 24-Jan-13 12:02pm
Well, i will try and tell you ^_^
leana88 25-Jan-13 4:15am
i failed to do it : (
by the way , i use gridview to display data and i want to delete from gridview.
Member 9581488 25-Jan-13 8:58am
You want to delete it when you click on delete in gridview but the image that is displayed on gridview is just one image file or multiple because in your question you have mentioned relation between 2 tables.
leana88 25-Jan-13 12:33pm
Hi ^_^ ,
gridview display elements table1 and each element in the table1 related with the elements in the table2 , i want when click delete on any element
in gridview delete this element with picture And delete records and images that are linking with it in the table2.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="pid" DataSourceID="SqlDataSource2" Width="460px">
<asp:BoundField DataField="pid" HeaderText="pid" InsertVisible="False"
ReadOnly="True" SortExpression="pid" />
<asp:BoundField DataField="pname" HeaderText="pname" SortExpression="pname" />
<asp:BoundField DataField="sum" HeaderText="sum" ReadOnly="True"
SortExpression="sum" />
<asp:Button ID="Button4" runat="server" Height="29px" onclick="Button4_Click"
style="font-weight: 700; font-size: medium" Text="Delete" Width="183px" />

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MySampleDBConnectionString %>"
SelectCommand="SELECT COUNT( AS sum,, table1.pname, table1.pimages, AS Expr1 FROM table1 LEFT OUTER JOIN table2 ON = GROUP BY,, table1.pname, table1.pimages">
Member 9581488 25-Jan-13 13:07pm
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="pid" DataSourceID="SqlDataSource2" OnRowDeleting="GridView1_RowDeleting" Width="460px">

<asp:Button ID="Button4" runat="server" Height="29px" CommandName="Delete"
style="font-weight: 700; font-size: medium" Text="Delete" Width="183px" />

in code behind:-
protected void gvAcceptanceStatus_RowDeleting(object sender, GridViewDeleteEventArgs e)
// Access your PID here using datakeynames (I told you in Your earlier post)

// Your sql code.
// Gather all the files names using sql select query passing PID & save it in dataset.
// Pass that PID in your SQL delete query (remember you have to join two tables because you have relation defined on that table)
// Execute your sql statement and Bind Grid Again

// Here delete files ( records from dataset) in your folder.

let me know if it does not work for you.
leana88 25-Jan-13 13:48pm
I could not do it : (
 Because it is difficult.
(Я буду плакать) : ((( !!!!!!
i will close this question.
Thanks for the help.
Member 9581488 25-Jan-13 16:29pm
No its not that difficult as you think. I want to help you out.
is there any other way we can communicate?
please dont cry!!!!!
leana88 26-Jan-13 16:14pm
Yes, we can communicate by email :
leana88 26-Jan-13 18:15pm
Yes, we can communicate by email :
I'm waiting you because, i need help in this topic ^_^
Member 9581488 26-Jan-13 19:04pm
Please wait i wil b online in a bit
Member 9581488 26-Jan-13 19:10pm
I added you in yahoo....
please accept req

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100