I have a DataGrid in my wpf window, and it's got a ContextMenu with items Update and Delete
When Update is Clicked the selected item's Name, UserName etc... is shown in TextBoxes of the window, so that the user can change them (when the user clicks on a addUserBtn the changes are saved to database)
Now,
one of the User's properties is thier Image, and i show that image using Image
element in WPF. the source of the image is by default a file inside the project. but when Update
is clicked, the ImageSource
becomes the User's Image's address (gotten from database)
I've used the code below to Copy the image file into the application's executable path, and then save the file's address in the database
string SavePic(string UserName)
{
string path = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\UserPictures\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string picName = UserName + ".jpg";
try
{
string picPath = ofd.FileName;
File.Copy(picPath, path + picName, true);
}
catch (Exception e)
{
MyMessageBox mb = new MyMessageBox("System was unable to save the picture\n" + e.Message);
mb.ShowDialog();
}
return path + picName;
}
So far so good
But the problem is when i try to save the changes (after clicking on Update
ContextMenu).
Weather I change the image or not, i get an error
Let's say I only want to change the User's Name, but the image is untouched, then I get this error .Empty file name is not legal Parameter name: sourceFileName
And if I change the image, I get this other error => The process cannot access the file 'C:Users
etc....because it is being used by another process
Just in case, this is the code, behind Update
ContextMenu button:
flagUpdate = true;
User temp = bll.Read(idValue);
txtName.Text = temp.Name;
txtUsername.Text = temp.UserName;
if (temp.Picture != null)
{
ProfilePicImage.Source = new BitmapImage(new Uri(temp.Picture));
}
else
{
ProfilePicImage.Source = new BitmapImage(new Uri("pack://application:,,,/Image/UserWindow/UserProfileImage.png"));
}
And this is the code behind addUserBtn
which saves the new values to the database:
User tempId = bll.Read(idValue);
User temp = new User();
temp.Name = txtName.Text;
temp.UserName = txtUsername.Text;
temp.Picture = SavePic(txtUsername.Text);
if (txtPassword.Text != "")
{
temp.Password = txtPassword.Text;
}
string action = bll.Update(temp, idValue);
ProfilePicImage.Source = new BitmapImage(new Uri("pack://application:,,,/Image/UserWindow/UserProfileImage.png"));
flagUpdate = false;
MyMessageBox mb = new MyMessageBox(action);
OpenWindow(mb);
DataGridFill();
it got very long :)
just wanted to give you all the information just in case, since I myself am clueless about what I should do