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);
}