Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christoffer Frede 30 posts 140 karma points
    Jun 01, 2021 @ 09:54
    Christoffer Frede
    0

    npoco insert entry from frontend template

    Hi all. im currently trying to add some data to a custom table using NPOCO using the docs for composers: https://our.umbraco.com/documentation/Extending/Database/ i have succesfully created my custom table using a composer/migration. and now i want to insert some data to the table from a page razor template, but i cant find any info on how to do this. i found this blog where there is examples inserting data into the npoco table: https://www.talkingdotnet.com/use-npoco-orm-with-asp-net-core/ but i was wondering if there is some built in umbraco way to get the database instead ?

  • Huw Reddick 1736 posts 6076 karma points MVP c-trib
    Jun 01, 2021 @ 10:05
    Huw Reddick
    0

    Hi Christoffer,

    Here is some example code for saving data to a custom table in Umbraco. It is a fairly simple config table, but should give you a starting point.

    public static void StoreValue(string name, string value)
    {
        var foundIt = GetValue(name);
    
    
        ConfigData config = new ConfigData
        {
            FieldName = name,
            Value = value
        };
        using (var scope = Current.ScopeProvider.CreateScope())
        {
            var database = scope.Database;
            if (foundIt != null)
            {
                foundIt.Value = value;
                database.Save<ConfigData>(foundIt);
            }
            else
            {
                database.Insert<ConfigData>(config);
            }
    
            scope.Complete();
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft