I am trying to update a database entry with mixed results

Grime 786 Reputation points
2021-10-22T07:10:33.18+00:00

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);
        }
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,446 Reputation points Microsoft Vendor
    2021-11-02T07:36:45.7+00:00

    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.


0 additional answers

Sort by: Most helpful