Hi friends,
I have been developing a windows app which is related to student details
administration, in which what i have done is i've create a form named "Add
Student" where i will add all the details of the student (like name, id,
address....) which also includes his image through a picture box. While saving
i have coded so that all the info will be saved into an ms access database
except image because of size limitations of access (ms access database
limitation is only 2 GB). In this i have also added a form so that any student
info can be edited. Here what i am doing is i am retrieving the particular
student info from access and assigning each and every detail to the edit form
which also included a pic box (Here pic will be assigned from the folder where
it is stored earlier). I think you've got my application functionality
right.
Now what my actual problem is, when the user wants to edit the pic, he will
selects another by browsing, then the picture he selected should be replaced
with the latest one but the name of the picture should remain same in the
folder (Here i have written code so that the picture name will be the student
Id to identify uniquely) now when i click the save button, i am getting an
error
"CLS10A14.JPG' because it is being used by another process"
I think this is because i am displaying the image in the picture box
initially and trying to edit it with the new one he selected.....
Here is the code snippets: To display details initially i have written
like this:
DataTable dtInfo = dcs.getTable();
drow = dtInfo.Rows[0];
lblStudentId.Text = drow["StudentId"].ToString();
txtName.Text = drow["Name"].ToString();
cbClass.SelectedValue = drow["Class"];
cbSection.SelectedValue = drow["Section"];
pbImage.Image = Image.FromFile(Application.StartupPath +
"\\Images\\"+ drow["image_path"].ToString());
it worked properly and displaying all info without any problem. Now the user
can change the image and clicks on save button
now i've written code like this
string updateQuery = "update StudentInformation set [Name] = '" +
txtName.Text
+ "',[Class] = '" + cbClass.SelectedValue
+ "',[Section] = '" + cbSection.SelectedValue
+ "',[image_path] = '" + txtImagePath.Text
+ "' where [StudentId] = '" + cbStudentId.SelectedItem.ToString() +
"'";
But here i have nothing to do with this query as i have already executed it
without any problem but after this i have to update the image in the images
folder with the new image but with the same name it has earlier (i mean if the
older image has a name like CLS10A14.JPG i want to delete it first and copy the
new image with the same name which is CLS10A1.JPG)
I have written code like this to achieve this but got the error told in the
beginning....
cmd = new OleDbCommand(updateQuery, con);
con.Open();
da.UpdateCommand = cmd;
int n = cmd.ExecuteNonQuery();
if (n == 1)
{
File.Delete(Application.StartupPath + "\\Images\\" +
txtImagePath.Text);
string newPath = Application.StartupPath + "\\Images\\" +
txtImagePath.Text;
File.Copy(imagename, newPath);
//File.Replace(imagename, newPath, cbStudentId.SelectedItem.ToString());
}
Actually my application is a Windows Application
Please help anybody knows solution for it thanks in advance....
Loading
Jaish MathewsPosted May 24, 2010, 12:35 PM
This is a valid error. Because your picture box already using that image, then how can u delete it.
1st break that binding with the picture box using below code
pbImage.Image.Dispose();
Then you are free to delete the image.
Praveen KumarPosted May 25, 2010, 1:13 AM
Thanks for your help, it worked.
Thanks a lot....
Praveen KumarPosted May 24, 2010, 10:20 AM
I have used it but no use getting same error....
Albertto LiePosted May 24, 2010, 7:35 AM
block the code File.Delete(Application.StartupPath + "\\Images\\" + txtImagePath.Text);
and try File.Copy(imagename, newPath, true);
its an option to overwrite the old file.