Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cant  delete image  because it used by anather process

Posted on 2009-07-03
5
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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