question

Grime avatar image
0 Votes"
Grime asked Grime commented

I am trying to update a database entry with mixed results

I have an update page that allows you to change the Name, Phone, Email and Avatar of an "Operator" stored in a SQLite database.
If the avatar button is selected, I can choose an image from the device which, on "OK", is stored to the database.
If I choose "OK" WITHOUT changing the existing avatar, it is lost.
I just need help in my logic to accept the displayed image and path if it is not changed.

Here is the code behind that tries to handle this:

         private async void OKButton_Clicked(object sender, EventArgs e)
         {
                
             var todoItem = (OperatorModel)BindingContext;
             Preferences.Get("LastAvatar", PhotoPath);
             if (PhotoPath != null)
             {
                 todoItem.OperatorAvatar = PhotoPath;
             }
                
             todoItem.OperatorAvatar = PhotoPath;
             HFNDatabase database = await HFNDatabase.Instance;
             await database.SaveItemAsync(todoItem);
    
             Preferences.Set("LastAvatar", null);
    
             EventHandler<OperatorModel> handler = ReturnValue;
             if (handler != null)
             {
                 handler(this, todoItem);
             }
             await Navigation.PopModalAsync();
         }
    
         private async void AvatarButton_Clicked(object sender, EventArgs e)
         {
             await TakePhotoAsync();
         }
    
         string PhotoPath;
    
         async Task TakePhotoAsync()
         {
             try
             {
                 var photo = await MediaPicker.PickPhotoAsync();
                 await LoadPhotoAsync(photo);
                 var stream = photo.OpenReadAsync();
                 // AvatarImage.Source = ImageSource.FromStream(() => stream);
                 // await DisplayAlert("in", PhotoPath, "OK");
                 AvatarButton.Text = PhotoPath;
                 AvatarButton.TextColor = Color.DarkBlue;
                 AvatarButton.FontSize = 14;
                 AvatarImage.Source = PhotoPath;
                 string NewOpName = NameEntry.Text;
                 string NewOpAvatar = AvatarButton.Text;
                 Preferences.Set("LastAvatar", PhotoPath);
    
             }
    
             catch (FeatureNotSupportedException fnsEx)
             {
                 // Feature is not supported on the device
             }
             catch (PermissionException pEx)
             {
                 // Permissions not granted
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
             }
         }
    
         async Task LoadPhotoAsync(FileResult photo)
         {
             // canceled
             if (photo == null)
             {
                 PhotoPath = null;
                 return;
             }
             // save the file into local storage
             var newFile = Path.Combine(FileSystem.AppDataDirectory, photo.FileName);
             using (var stream = await photo.OpenReadAsync())
             using (var newStream = File.OpenWrite(newFile))
             await stream.CopyToAsync(newStream);
             PhotoPath = newFile;
    
             AvatarButton.Text = PhotoPath;
             AvatarButton.TextColor = Color.DarkBlue;
             AvatarButton.FontSize = 14;
             AvatarImage.Source = PhotoPath;
             string NewOpName = NameEntry.Text;
             string NewOpAvatar = AvatarButton.Text;
             Preferences.Set("LastAvatar", PhotoPath);
         }
     }
 }
dotnet-xamarin
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

"it is lost", you mean the PhotoPath is null in your database or the path is correct however the image doesn't display? In this line of code, we can see that you get the key - LastAvatar

  Preferences.Get("LastAvatar", PhotoPath);

However, you don't give it to this variable string PhotoPath, you also save null for this key.

   Preferences.Set("LastAvatar", null);

You could try to use the following code

 PhotoPath = Preferences.Get("LastAvatar", PhotoPath);
  Preferences.Set("LastAvatar", PhotoPath );

In addition, I can't reproduce this issue according to the code you provided, would you mind sharing a minimal reproducible demo? You could upload it to gitHub, and attach the link here.


0 Votes 0 ·
Grime avatar image Grime WenyanZhang-MSFT ·

Here you go... It's private but I'm happy to have you contribute.

https://github.com/VizMAP/HandsFreeNotes4

0 Votes 0 ·

It is 404 no found. May I ask do you try to set the value to PhotoPath ?

  PhotoPath = Preferences.Get("LastAvatar", PhotoPath);
0 Votes 0 ·
Show more comments

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered Grime commented

Hello,

Welcome to our Microsoft Q&A platform!

You could change the OKButton_Clicked method by the following code, it means that when the PhotoPath is not equle to null ,save it. After the database update is complete, set LastAvatar to empty.

 private async void OKButton_Clicked(object sender, EventArgs e)
         {
             var todoItem = (OperatorModel)BindingContext;
             PhotoPath = Preferences.Get("LastAvatar", null);
             if (PhotoPath != null)
             {
                 todoItem.OperatorAvatar = PhotoPath;
                 HFNDatabase database = await HFNDatabase.Instance;
                 await database.SaveItemAsync(todoItem);
                 Preferences.Set("LastAvatar", null);
             }
             EventHandler<OperatorModel> handler = ReturnValue;
             if (handler != null)
             {
                 handler(this, todoItem);
             }
             await Navigation.PopModalAsync();
         }


Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That seems logical, but if I update the Operators by adding Avatars, select OK then close the app, when I re-deploy the app from Visual Studio and go to Select Operator there are no avatars, even though the Operator's Names, Phones and Emails are persistent.

0 Votes 0 ·

After clicking the ok button, the " Update Operator" page will drop down. May I ask do you close the app after the page has droped down? I can't reproduce this issue.

0 Votes 0 ·
Grime avatar image Grime WenyanZhang-MSFT ·

First of all I add a few Operators, including their avatars, then when I choose Select Operator, they are all there with their avatars:

146019-img-8752.jpg

I then recompile the app onto my iOS device and immediately go to Select Operator, and the Operators don't have avatars:

146027-img-8753.jpg


0 Votes 0 ·
img-8752.jpg (177.8 KiB)
img-8753.jpg (93.2 KiB)
Show more comments