Solved

cant  delete image  because it used by anather process

Posted on 2009-07-03
5
243 Views
Last Modified: 2013-12-17
i cant delete the image after i process to DB ,i dont know how to release it .please look to the code.
thank you
private void btnSave_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo("C:/MyDir");
            FileInfo[] rgFiles = di.GetFiles("*.jpg");
            foreach (FileInfo fi in rgFiles){
            try
            {
                //Read Image Bytes into a byte array
                byte[] imageSampleData = ReadImageToBytes(fi.FullName);
 
                //Initialize SQL Server Connection
                SqlConnection con = new SqlConnection(txtConnectionString.Text);
 
                //Set insert query
                string query = "INSERT INTO tblImages (FullPath,MyImageSample) values(@FullPath, @MyImageSample)";
 
                //Initialize SqlCommand object for insert.
                SqlCommand cmd = new SqlCommand(query, con);
 
                cmd.Parameters.Add(new SqlParameter("@FullPath",
                                            (object)fi.FullName));
 
                cmd.Parameters.Add(new SqlParameter("@MyImageSample",
                                                    (object)imageSampleData));
 
                //Open connection and execute insert query.
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
               
                
            }
            catch
            {
                MessageBox.Show("Error while saving image.", "Error");
            }
            fi.Delete();

Open in new window

0
Comment
Question by:mark222
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24772347
Do you want to delete all the images from the directory ?

By the way, fi.Delete() --> which file do you want to delete, because it's outside the loop. It will always contain the latest file :)
0
 

Author Comment

by:mark222
ID: 24772517
thank Dhaest, no it is inside the loop but forget to copy the last char }.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24772541
You can try the following. Add the following items behind con.Close();
(probably one of those will block you)
imageSampleData  = null;
cmd = null;
0
 

Author Comment

by:mark222
ID: 24772565
not working , still same problem
0
 

Accepted Solution

by:
mark222 earned 0 total points
ID: 24772672
it is solved ,the problem because  FileStream fStream was not close.
thank you Dhaest
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now