Copied to clipboard

Flag this post as spam?

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


  • Jonathan Ben Avraham 43 posts 216 karma points
    Apr 26, 2016 @ 12:24
    Jonathan Ben Avraham
    0

    Database Query not executing

    I have a package controller set up in app_code and the code isn't throwing any errors or exceptions, I'm trying to import data from a CSV into an Umbraco database table.

    please note that the method we are looking at is: SaveLT()

    here is my controller:

    using UmbracoImportExportPlugin.Models;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Persistence;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    
    namespace UmbracoImportExportPlugin.App_Code
    {
    
        public class ImportNewDictionaryController : UmbracoAuthorizedApiController
        {
            public string basePath;
    
            //Locate specific path
            public void LocatePath()
            {
                this.basePath = System.Web.Hosting.HostingEnvironment.MapPath(@"/upload");
            }
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            //[System.Web.Http.HttpPost]
            public void SaveFile()
            {
                var myContext = Request.TryGetHttpContext();
                List<string> keys = new List<string>();
                if (myContext.Success)
    
                {
                    HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
                    if (myFile == null)
                    {
                       throw new HttpException("invalid file");
                    }
                    else
                    {
                        StreamReader csvreader = new StreamReader(myFile.InputStream);
    
    
                        while (!csvreader.EndOfStream)
                        {
                            var line = csvreader.ReadLine();
                            if (line != "Key")
                            keys.Add(line);
                        }
                    }
                    UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                    var remove = new Sql("DELETE FROM cmsDictionary");
                    int rem = db.Execute(remove);
    
                    foreach (string item in keys)
                    {
                        var insert = new Sql("INSERT INTO cmsDictionary VALUES (NEWID(), null,'" + item + "')");
                        int res = db.Execute(insert);
                    }
                }
            }
    
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            public void SaveLT()
            {
                List<int> id = new List<int>();
                var myContext = Request.TryGetHttpContext();
                List<string> data = new List<string>();
                if (myContext.Success)
                {
                    HttpPostedFileBase myFile = myContext.Result.Request.Files["file"];
                    if (myFile == null)
                    {
                        throw new HttpException("invalid file");
                    }
                    else
                    {
                        StreamReader csvreader = new StreamReader(myFile.InputStream);
    
    
                        while (!csvreader.EndOfStream)
                        {
                            var line = csvreader.ReadLine();
                            if (line != "Value")
                                data.Add(line);
                        }
                    }
                    UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                    var remove = new Sql("DELETE FROM cmsLanguageText");
                    int rem = db.Execute(remove);
                    for (var i = 1; i < 142; i++ )
                    {
                         foreach (string lang in data)
                        {
                            foreach (int ident in id)
                            {
                                var insertNew = new Sql("INSERT INTO cmsLanguageText (languageId, UniqueId, value) VALUES (" + ident + " , NEWID() , '" + lang + "')");
                                int res = db.Execute(insertNew);
                            }
                        }
                    }
                }
            }
            public List<int> getList()
            {
                UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
                var select = new Sql("SELECT [id] FROM umbracoLanguage;");
                List<int> id = new List<int>();
                id = db.Fetch<int>(select);
                return id;
            }
    
            public String GetUserName()
            {
                var current = UmbracoContext.Current;
                var user = current.UmbracoUser;
                return user.Name.ToString();
            }
        }
    }
    

    This is running fine and I even get the alert set up in my angular controller but it's just not updating the database table. Any suggestions as to why it might not be doing this?

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 26, 2016 @ 13:01
    Steve Morgan
    0

    Hi,

    you create an empty List

    foreach (string ident in id)
                        {
                            Int32.Parse(ident);
                            var insertNew = new Sql("INSERT INTO cmsLanguageText (languageId, UniqueId, value) VALUES (" + ident + " , NEWID() , '" + lang + "')");
                            int res = db.Execute(insertNew);
                        }
    

    Is never executed. Are you supposed to add some language Ids here?

  • Jonathan Ben Avraham 43 posts 216 karma points
    Apr 26, 2016 @ 13:03
    Jonathan Ben Avraham
    0

    Yeah, I'm trying to loop in the id's from the umbracoLanguage table.

    hence the:

    public List<int> getList()
        {
            UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
            var select = new Sql("SELECT [id] FROM umbracoLanguage;");
            List<int> id = new List<int>();
            id = db.Fetch<int>(select);
            return id;
        }
    
  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 26, 2016 @ 13:15
    Steve Morgan
    0

    Yes but you don't call that function...

    Not clearthat the SaveLT is being called (I assume this externally done?) but line

    List<int> id = new List<int>();  
    

    should be

    List<int> id = getList();
    
  • Jonathan Ben Avraham 43 posts 216 karma points
    Apr 26, 2016 @ 13:53
    Jonathan Ben Avraham
    0

    So I've fixed that issue, but now the database is throwing me this error:

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_cmsLanguageText_cmsDictionary_id". The conflict occurred in database "UmbracoImportExportPlugin", table "dbo.cmsDictionary", column 'id'.
    The statement has been terminated.
    

    I'm new to C# and Umbraco, any suggestions about this?

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 26, 2016 @ 14:07
    Steve Morgan
    100

    Hi,

    To be honest I'm not sure what that table does - generally it's a bad idea to write directly to Umbraco tables unless you know what you're doing.

    I assumed that was a custom table in your code - I should have spotted it earlier that it's an Umbraco table.

    The warning you're getting is that there is a foreign key constraint on the table - e.g. you're writing to one table that is referenced by another (in other words you're about to get your data horribly messed up).

    What is it you're trying to do - I suspect there is an easier and cleaner way via an API. Is it to add dictionary keys?

    If so try this guys approach

    https://our.umbraco.org/forum/developers/api-questions/71292-saving-translation-dictionary-via-api

    Steve

  • Jonathan Ben Avraham 43 posts 216 karma points
    Apr 27, 2016 @ 15:26
    Jonathan Ben Avraham
    0

    That worked perfectly! Thank you

Please Sign in or register to post replies

Write your reply to:

Draft