Copied to clipboard

Flag this post as spam?

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


  • QBank 17 posts 108 karma points
    May 06, 2019 @ 10:26
    QBank
    0

    Insert/Update data to custom table not work

    I have created a custom table in database and try to input data into it from within Umbraco code but it is not updating or inserting data.

    but selection of data works fine.

  • Marcio Goularte 388 posts 1360 karma points
    May 06, 2019 @ 11:40
    Marcio Goularte
    0

    could you show the code?

  • QBank 17 posts 108 karma points
    May 07, 2019 @ 06:49
    QBank
    0

    Following is the code .

    using (var db = _scopeProvider.CreateScope().Database)
                {
    
                    if (postValue.Id > 0)
                    {
                        db.Save(postValue);
                    }
                    else
                    {
                        db.Insert<QBankSetting>(postValue);
                    }
    
                };
    
  • Jonathan Distenfeld 105 posts 618 karma points
    May 07, 2019 @ 09:50
    Jonathan Distenfeld
    0

    Hi Salinda,

    this looks fine to me. Do you get any errors?

  • QBank 17 posts 108 karma points
    May 08, 2019 @ 02:42
    QBank
    0

    No error it just do not update or insert data into database .

    in other word it doesn't commit changes to database once code run.

    but i tried entering data into database directly using MSSQL management Studio and try retrieve data using code and it worked but insertion/update not working.

  • Lasse 49 posts 161 karma points
    May 08, 2019 @ 05:13
    Lasse
    1

    I don't have much knowledge about this topic, but isn't there a .SaveChanges() or a .Flush(), or something that you need to pull at the end, to apply the changes to the db?

  • QBank 17 posts 108 karma points
    May 08, 2019 @ 05:39
    QBank
    0

    No SaveChange() or similar commend that i can find in npoco package.

    npoco is the microORM that used with umbraco v8.

    i able insert to table after much effort by duplicating code to data insertion twice as below;

     try
            {
                using (var db = _scopeProvider.CreateScope().Database)
                {
                    db.Insert(postValue);
                };
                using (var db = _scopeProvider.CreateScope().Database)
                {
                    db.Insert(postValue);
                };
            }
    

    but not a nice solution.

  • Jonathan Distenfeld 105 posts 618 karma points
    May 08, 2019 @ 07:08
    Jonathan Distenfeld
    0

    Hi Salinda,

    just so i get you right. This Code works:

    try
    {
       using (var db = _scopeProvider.CreateScope().Database)
       {
          db.Insert(postValue);
       };
       using (var db = _scopeProvider.CreateScope().Database)
       {
          db.Insert(postValue);
       };
    }
    

    while this Code doesn't?

    try
    {
       using (var db = _scopeProvider.CreateScope().Database)
       {
          db.Insert(postValue);
       };
    }
    

    Could you please provide the code of the model your trying to insert?

    Jonathan

  • QBank 17 posts 108 karma points
    May 08, 2019 @ 07:28
    QBank
    0

    yes it is as you understood.!

    model I am trying to insert is;

    [TableName("QBankSettings")]
    [PrimaryKey("Id")]
    public class QBankSetting
    {
        public QBankSetting()
        {
    
        }
    
        public int Id { get; set; }
        public string Username { get; set; }
    
        [NullSetting(NullSetting = NullSettings.Null)]
        public string UmbracoUsername { get; set; }
    
        [NullSetting(NullSetting = NullSettings.Null)]
        public string UmbracoPassword { get; set; }
    
        [NullSetting(NullSetting = NullSettings.Null)]
        public string TinyTemplateImage { get; set; }
        public bool IsQBankPersonalSettingsActive { get; set; }
        public string Password { get; set; }
        public string ServiceUrl { get; set; }
        public string ClientId { get; set; }
        public int DeploymentId { get; set; }
        public int UsageSourceId { get; set; }
    }
    
  • Jonathan Distenfeld 105 posts 618 karma points
    May 08, 2019 @ 07:51
    Jonathan Distenfeld
    0

    How do you handle the primary key? Do you set it yourself?

    Not sure if this is releated, but NPoco has to know how to handle the primary key. Also it might be the mapping is not working correctly.

    This is how I usually set up such models:

    [TableName("QBankSettings")]
    [PrimaryKey("Id", AutoIncrement = true)]
    [ExplicitColumns]
    public class QBankSetting
    {
        public QBankSetting()
        {
    
        }
    
        [Column("Id")]
        [PrimaryKeyColumn(AutoIncrement = true)]
        public int Id { get; set; }
    
        [Column("Username")]
        public string Username { get; set; }
    
        [Column("UmbracoUsername")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public string UmbracoUsername { get; set; }
    
        [Column("UmbracoPassword")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public string UmbracoPassword { get; set; }
    
        [Column("TinyTemplateImage")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public string TinyTemplateImage { get; set; }
    
        [Column("IsQBankPersonalSettingsActive")]
        public bool IsQBankPersonalSettingsActive { get; set; }
    
        [Column("Password")]
        public string Password { get; set; }
    
        [Column("ServiceUrl")]
        public string ServiceUrl { get; set; }
    
        [Column("ClientId")]
        public string ClientId { get; set; }
    
        [Column("DevelopmentId")]
        public int DeploymentId { get; set; }
    
        [Column("UsageSourceId")]
        public int UsageSourceId { get; set; }
    }
    

    It's just a guess but you could give it a try. Might be you have to recreate the database from model..

    Jonathan

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    May 08, 2019 @ 08:13
    Richard Soeteman
    0

    You need to complete te scope to make sure changes get perstisted to the database in V8, see this sample from Warren

    https://gist.github.com/warrenbuckley/ff06f8cf1ed98c15b7c7215abd460438

    Hope this helps,

    Richard

  • QBank 17 posts 108 karma points
    May 08, 2019 @ 09:30
    QBank
    0

    Thanks Richard for the link . I was able to insert/update data now .

  • Bo Jacobsen 608 posts 2406 karma points
    May 08, 2019 @ 10:48
    Bo Jacobsen
    0

    Hi Salinda.

    You can also use autoComplete

    using (var scope = scopeProvider.CreateScope(autoComplete: true))
    {
       .... 
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft