ContentSavingNotification + IScopeProvider
# help-with-umbraco
n
Hello, Anyone here have successfully communicated the database using ContentSavingNotification with IscopeProvider? If yes, I hope you can help. My issue was I'm trying to save data to the database once the save button clicked in the back office and what happened was there's no issue at all and nothing saved in the database. Hope someone can help. Thank you
Copy code
public void Handle(ContentSavingNotification notification)
{
    try
    {
        foreach (var node in notification.SavedEntities)
        {
            if (node.ContentType.Alias.Equals(CourseDetailChild.ModelTypeAlias))
            {

                string address = node.GetValue<string>("address");
                string map = node.GetValue<string>("map");


                string fullAddress = null;

                // Check if map is not null or empty
                if (!string.IsNullOrEmpty(map))
                {
                    try
                    {
                        // Try to parse map as JSON
                        var mapObject = JObject.Parse(map);
                        fullAddress = mapObject["address"]?["full_address"]?.ToString();
                    }
                    catch (Exception ex)
                    {
                        // Handle invalid JSON
                        // Log or handle exception if needed
                        fullAddress = null;
                    }
                }


                var result = _courseAddress.CheckDuplicateAddress(address, map).Result;

                if (result == null)
                {
                    CourseAddress model = new CourseAddress()
                    {
                        Address = address,
                        Map = fullAddress
                    };

                    _courseAddress.Save(model);
                }
            }
        }
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.ToString(), "CourseSaved");
    }
}
Copy code
public async Task Save(CourseAddress model)
{
    using (var scope = _scopeProvider.CreateScope(autoComplete: true))
    {
        var result = await scope.Database.InsertAsync(model);
        scope.Complete();
    }
}
a
Just a tip: If you put your code between triple ` then you'll get it better formatted even better if you add the language (like \```csharp
Copy code
csharp
public void Handle(ContentSavingNotification notification)
{
    try
    {
        foreach (var node in notification.SavedEntities)
        {
            if (node.ContentType.Alias.Equals(CourseDetailChild.ModelTypeAlias))
            {

                string address = node.GetValue<string>("address");
                string map = node.GetValue<string>("map");


                string fullAddress = null;

                // Check if map is not null or empty
                if (!string.IsNullOrEmpty(map))
                {
                    try
                    {
                        // Try to parse map as JSON
                        var mapObject = JObject.Parse(map);
                        fullAddress = mapObject["address"]?["full_address"]?.ToString();
                    }
                    catch (Exception ex)
                    {
                        // Handle invalid JSON
                        // Log or handle exception if needed
                        fullAddress = null;
                    }
                }


                var result = _courseAddress.CheckDuplicateAddress(address, map).Result;

                if (result == null)
                {
                    CourseAddress model = new CourseAddress()
                    {
                        Address = address,
                        Map = fullAddress
                    };

                    _courseAddress.Save(model);
                }
            }
        }
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.ToString(), "CourseSaved");
    }
}
public async Task Save(CourseAddress model)
{
    using (var scope = _scopeProvider.CreateScope(autoComplete: true))
    {
        var result = await scope.Database.InsertAsync(model);
        scope.Complete();
    }
}
5 Views