2

I'm a newbie on c# programming. I have a problem when I'm using FileStream. I want to retrieve picture from the database by searching ID of person in the database. AND IT WORKS!!. but when I try to retrieve picture of the person twice (inserting same ID twice). it will give IOException

"The process cannot access the file 'C:\Users\dor\Documents\Visual Studio 2010\Projects\studbase\studbase\bin\Debug\foto.jpg' because it is being used by another process."

I have 1 button --> buttonLoad | 1 pictureBox --> pictureBoxPictADUStudent

this is the code on buttonLoad

        string sql = "SELECT Size,File,Name FROM studgeninfo WHERE NIM = '"+textBoxNIM.Text+"'";
        MySqlConnection conn = new MySqlConnection(connectionSQL);
        MySqlCommand comm = new MySqlCommand(sql, conn);

        if (textBoxNIM.Text != "")
        {
            conn.Open();
            MySqlDataReader data = comm.ExecuteReader();

            while (data.Read())
            {

                int fileSize = data.GetInt32(data.GetOrdinal("size"));
                string name = data.GetString(data.GetOrdinal("name"));


                using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
                {
                        byte[] rawData = new byte[fileSize];
                        data.GetBytes(data.GetOrdinal("file"), 0, rawData, 0, fileSize);
                        fs.Write(rawData, 0, fileSize);
                        fs.Dispose();
                        pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);
                }
            }

            conn.Close();
        }

        else
        {
            MessageBox.Show("Please Input Student NIM ", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
Jonathan Day
  • 18,519
  • 10
  • 84
  • 137

3 Answers3

4

You are opening the file here:

using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
                                   // ^^^^ This is your filename..

..and Bitmap is also trying to open the file to read it.. here:

pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);
                                                   // ^^^^ You are using it again here

The Bitmap won't be able to read from the file while you're writing to it..

EDIT:

As pointed out in the comments, this can happen even though fs.Dispose() is being called. See here: Does FileStream.Dispose close the file immediately?

Community
  • 1
  • 1
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

The problem here is that the file is locked by new Bitmap(). So you have to make sure once the bitmap is loaded, it's lock on the file is disposed:

        using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
        {
                byte[] rawData = new byte[fileSize];
                data.GetBytes(data.GetOrdinal("file"), 0, rawData, 0, fileSize);
                fs.Write(rawData, 0, fileSize);
        }

        using (var bmpTemp = new Bitmap(name))
        {
            pictureBoxPictADUStudent.BackgroundImage= new Bitmap(bmpTemp);
        } 

Update: I updated my answer to reflect your latest answers. For mor detailed information goto this post

Community
  • 1
  • 1
DerApe
  • 3,097
  • 2
  • 35
  • 55
  • The fs.Dispose() is now redundant and can be removed. – Polyfun Jan 31 '13 at 11:52
  • i have tried it. but it always give me IOException Error just like before. – eliezeramon Jan 31 '13 at 12:08
  • for first searching, it is work. but when i search once again (without close the application) it give me the same error – eliezeramon Jan 31 '13 at 12:09
  • @eliezeramon are you sure the ressource is not accessed at any other place? – DerApe Jan 31 '13 at 12:30
  • i removed it. but still give IOException on FileStream fs = new FileStream(name,FileMode.Create,FileAccess.Read); – eliezeramon Jan 31 '13 at 12:33
  • @derape . i will explain the proses. i have save image into database. and when i try to retrieve it using FileStream, in my folder bin/debug appear the picture from DB. in my database have 2 ID with two pict. 08520060 and 0852061. when i search 08520060, it works. pict appear. when i search the second ID(08520061) it works. but when i search ID 08520060 again. the IOException appear. – eliezeramon Jan 31 '13 at 12:38
  • @eliezeramon ah okay, is it possible that the "old" image is still assigned to the picturebox, thus it's still accessing the image? try `pictureBoxPictADUStudent.BackgroundImage = null;` before using the filestream – DerApe Jan 31 '13 at 12:54
  • @derape is this right? while (data.Read()) { pictureBoxPictADUStudent.BackgroundImage = null; using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write)) { } pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name); } – eliezeramon Jan 31 '13 at 14:14
-1

write like this using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))

{


} pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);