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?
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;
}
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?
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?
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:
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?
Hi,
you create an empty List
Is never executed. Are you supposed to add some language Ids here?
Yeah, I'm trying to loop in the id's from the umbracoLanguage table.
hence the:
Yes but you don't call that function...
Not clearthat the SaveLT is being called (I assume this externally done?) but line
should be
So I've fixed that issue, but now the database is throwing me this error:
I'm new to C# and Umbraco, any suggestions about this?
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
That worked perfectly! Thank you
is working on a reply...