Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,
 
i have two tables stored images path, and images are stored in a folder,
 
table1
------
pid int
pname nvarchar(50)
pimages nvarchar(max)
-----------------------------------
table2
------
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.... ^_^
 
Thanks.
Posted 23-Jan-13 4:33am
leana88616

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
leana88 at 23-Jan-13 10:52am
   
ok , and the images How to delete it from folder ?
Member 9581488 at 23-Jan-13 10:55am
   
Please refer below link:-
http://msdn.microsoft.com/en-us/library/cc148994.aspx
You can pass your PATH(where your file is saved) to find if the file exists, if it does then Delete it!!!!!
leana88 at 23-Jan-13 11:01am
   
Well, i will try with this link.
Thank you ^_^
Member 9581488 at 23-Jan-13 11:07am
   
let me know if you cant do it.
I will def help you out.
leana88 at 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 at 24-Jan-13 11:33am
   
For deleting in database or in folder?
leana88 at 24-Jan-13 11:45am
   
only delete from folder... because this is my problem.
Member 9581488 at 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 at 24-Jan-13 12:02pm
   
Well, i will try and tell you ^_^
leana88 at 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 at 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 at 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:TemplateField>

<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(table2.pid) AS sum, table1.pid, table1.pname, table1.pimages, table2.pid AS Expr1 FROM table1 LEFT OUTER JOIN table2 ON table1.pid = table2.pid GROUP BY table2.pid, table1.pid, table1.pname, table1.pimages">
Member 9581488 at 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 at 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 at 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 at 26-Jan-13 16:14pm
   
Hi,
Yes, we can communicate by email : marina_skova21@yahoo.com
^_^
leana88 at 26-Jan-13 18:15pm
   
Hi,
Yes, we can communicate by email : marina_skova21@yahoo.com
I'm waiting you because, i need help in this topic ^_^
Member 9581488 at 26-Jan-13 19:04pm
   
Please wait i wil b online in a bit
Member 9581488 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
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