Sql-server Query Not Executing Correctly In Umbraco C# Controller
So my code seems to be running fine, no exceptions or errors being thrown, however when I check my db table after the 'import complete' alert jumps, there is nothing there. PLEASE
Solution 1:
You set List<string> id = new List<string>()
and then you do a foreach (string ident in id)
. As id
is an empty list you have nothing to iterate through and so your INSERT statement is never reached.
Try populating id
with some values to iterate through.
Unless I've misread your code in my quick read-through.
Solution 2:
Rather than try SQL and insert it manually into the database, Umbraco actually has a Localization Service and model for this:
[System.Web.Http.AcceptVerbs("GET", "POST")]
publicvoidSaveLT()
{
varls= ApplicationContext.Current.Services.LocalizationService;
//create a holder for the item's DictionaryTranslations
List<DictionaryTranslation> _hello = newList<DictionaryTranslation>();
List<DictionaryTranslation> _submit = newList<DictionaryTranslation>();
List<DictionaryTranslation> _form = newList<DictionaryTranslation>();
List<DictionaryTranslation> _bootstrap = newList<DictionaryTranslation>();
List<DictionaryTranslation> _world = newList<DictionaryTranslation>();
List<DictionaryTranslation> _heaven = newList<DictionaryTranslation>();
List<DictionaryTranslation> _hell = newList<DictionaryTranslation>();
List<DictionaryTranslation> _this = newList<DictionaryTranslation>();
List<DictionaryTranslation> _sublime1 = newList<DictionaryTranslation>();
//the constructor for a DictionaryItem requires the Umbraco language object and value of the translated text //so get the language object, eg from Iso Code varlanguage= ls.GetLanguageByIsoCode("he-IL");
varlang1= ls.GetLanguageByIsoCode("ru");
varlang2= ls.GetLanguageByIsoCode("en-US");
// here we create a french translation for our item and add it to the list DictionaryTranslationhebhello=newDictionaryTranslation(language, "שלום");
DictionaryTranslationrushello=newDictionaryTranslation(lang1, "Здравствуйте");
DictionaryTranslationenghello=newDictionaryTranslation(lang2, "Blah");
_hello.Add(hebhello);
_hello.Add(rushello);
_hello.Add(enghello);
DictionaryTranslationhebsubmit=newDictionaryTranslation(language, "שלח");
DictionaryTranslationrussubmit=newDictionaryTranslation(lang1, "Отправить");
DictionaryTranslationengsubmit=newDictionaryTranslation(lang2, "Submit");
_submit.Add(hebsubmit);
_submit.Add(russubmit);
_submit.Add(engsubmit);
DictionaryTranslationhebform=newDictionaryTranslation(language, "טופס");
DictionaryTranslationrusform=newDictionaryTranslation(lang1, "форма");
DictionaryTranslationengform=newDictionaryTranslation(lang2, "Form");
_form.Add(hebform);
_form.Add(rusform);
_form.Add(engform);
DictionaryTranslationhebbtstrp=newDictionaryTranslation(language, "אֹזֶן הַנַעַל");
DictionaryTranslationrusbtstrp=newDictionaryTranslation(lang1, "начальная загрузка");
DictionaryTranslationengbtstrp=newDictionaryTranslation(lang2, "Bootstrap");
_bootstrap.Add(hebbtstrp);
_bootstrap.Add(rusbtstrp);
_bootstrap.Add(engbtstrp);
DictionaryTranslationhebworld=newDictionaryTranslation(language, "עוֹלָם");
DictionaryTranslationrusworld=newDictionaryTranslation(lang1, "Мир");
DictionaryTranslationengworld=newDictionaryTranslation(lang2, "World");
_world.Add(hebworld);
_world.Add(rusworld);
_world.Add(engworld);
DictionaryTranslationhebheaven=newDictionaryTranslation(language, "גן העדן");
DictionaryTranslationrusheaven=newDictionaryTranslation(lang1, "небо");
DictionaryTranslationengheaven=newDictionaryTranslation(lang2, "Heaven");
_heaven.Add(hebheaven);
_heaven.Add(rusheaven);
_heaven.Add(engheaven);
DictionaryTranslationhebhell=newDictionaryTranslation(language, "גגֵיהִנוֹם");
DictionaryTranslationrushell=newDictionaryTranslation(lang1, "ад");
DictionaryTranslationenghell=newDictionaryTranslation(lang2, "Hell");
_hell.Add(hebhell);
_hell.Add(rushell);
_hell.Add(enghell);
DictionaryTranslationhebthis=newDictionaryTranslation(language, "זֶה");
DictionaryTranslationrusthis=newDictionaryTranslation(lang1, "это");
DictionaryTranslationengthis=newDictionaryTranslation(lang2, "This");
_this.Add(hebthis);
_this.Add(rusthis);
_this.Add(engthis);
DictionaryTranslationhebsub=newDictionaryTranslation(language, "נִשׂגָב");
DictionaryTranslationrussub=newDictionaryTranslation(lang1, "возвышенный");
DictionaryTranslationengsub=newDictionaryTranslation(lang2, "Sublime");
_sublime1.Add(hebsub);
_sublime1.Add(russub);
_sublime1.Add(engsub);
//get or create a DictionaryItem, (passing in the Dictionary Key) IDictionaryItemhello= ls.DictionaryItemExists("hello_button") ? ls.GetDictionaryItemByKey("hello_button") : newDictionaryItem("hello_button");
IDictionaryItemsubmit= ls.DictionaryItemExists("submit_button") ? ls.GetDictionaryItemByKey("submit_button") : newDictionaryItem("submit_button");
IDictionaryItemform= ls.DictionaryItemExists("form_button") ? ls.GetDictionaryItemByKey("form_button") : newDictionaryItem("form_button");
IDictionaryItembtstrp= ls.DictionaryItemExists("bootstrap_button") ? ls.GetDictionaryItemByKey("bootstrap_button") : newDictionaryItem("bootstrap_button");
IDictionaryItemworld= ls.DictionaryItemExists("world_button") ? ls.GetDictionaryItemByKey("world_button") : newDictionaryItem("world_button");
IDictionaryItemheaven= ls.DictionaryItemExists("heaven_button") ? ls.GetDictionaryItemByKey("heaven_button") : newDictionaryItem("heaven_button");
IDictionaryItemhell= ls.DictionaryItemExists("hell_button") ? ls.GetDictionaryItemByKey("hell_button") : newDictionaryItem("hell_button");
IDictionaryItemThis= ls.DictionaryItemExists("this_button") ? ls.GetDictionaryItemByKey("this_button") : newDictionaryItem("this_button");
IDictionaryItemsublime= ls.DictionaryItemExists("sublime_button") ? ls.GetDictionaryItemByKey("sublime_button") : newDictionaryItem("sublime_button");
// set the translations created above
hello.Translations = _hello;
submit.Translations = _submit;
form.Translations = _form;
btstrp.Translations = _bootstrap;
world.Translations = _world;
heaven.Translations = _heaven;
hell.Translations = _hell;
This.Translations = _this;
sublime.Translations = _sublime1;
//now save the dictionary item and translations to Umbraco UmbracoDatabasedb= ApplicationContext.DatabaseContext.Database;
varremove=newSql("DELETE FROM cmsLanguageText");
intrem= db.Execute(remove);
ls.Save(hello);
ls.Save(submit);
ls.Save(form);
ls.Save(btstrp);
ls.Save(world);
ls.Save(heaven);
ls.Save(hell);
ls.Save(This);
ls.Save(sublime);
}
Everything has to be put in manually, however it does the job and as long as you know the syntax it's easy.
Post a Comment for "Sql-server Query Not Executing Correctly In Umbraco C# Controller"