public void btnCreate_Click(object sender, EventArgs e) { try { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { var newTest = new Test(); newTest.Name = txtNameOfTest.Text; newTest.Date = DateTime.Now; newTest.MainLang = cBMainLang.SelectedIndex + 1; newTest.SecLang = cBSecLang.SelectedIndex + 1; newTest.UserId = LoggedInUser.UserId; newTest.StatusId = 0; dbConnection.Test.Add(newTest); dbConnection.SaveChanges(); currentTestId = newTest.Id; } loadPanel(); } catch (EntityException ex) { MessageBox.Show("Could not add the test to database." + ex.Message); } catch (Exception ex) { MessageBox.Show("Error when adding to Database." + ex.Message + ex.InnerException); } }
void btnSave_Click(object sender, EventArgs e) { int a = 0; int b = 0; string[] mainLangArr = new string[n]; string[] secLangArr = new string[n]; var sortedTxtBoxes = panel.Controls.OfType <TextBox>().OrderBy(ctrl => ctrl.TabIndex).ToList(); foreach (var item in sortedTxtBoxes) { if (item.Name.Contains("txtMainLang")) { mainLangArr[a] = item.Text; //Debug.WriteLine("First ",a.ToString()); a++; } else if (item.Name.Contains("txtSecLang")) { secLangArr[b] = item.Text; // Debug.WriteLine("Second ", b.ToString()); b++; } } try { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { for (int i = 0; i < n; i++) { var newWord = new Word(); newWord.TestId = currentTestId; newWord.Word1 = mainLangArr[i]; newWord.Word2 = secLangArr[i]; dbConnection.Word.Add(newWord); } dbConnection.SaveChanges(); } } catch (EntityException ex) { MessageBox.Show("Could not save the test to database." + ex.Message); } catch (Exception ex) { MessageBox.Show("Error when saving to Database." + ex.Message + ex.InnerException); } finally { Close(); } }
private void btnSave_Click(object sender, EventArgs e) { int x = 0; int y = 0; string[] mainLangArrToDb = new string[numberOfWords]; string[] secLangArrToDb = new string[numberOfWords]; //Sorts all textbox objects to a variabel. var sortedTxtBoxes = tblPanel.Controls.OfType <TextBox>().OrderBy(ctrl => ctrl.TabIndex).ToList(); if (sortedTxtBoxes != null) { //Loops through all textboxes. foreach (var item in sortedTxtBoxes) { if (item.Name.Contains("txtMainLang")) { mainLangArrToDb[x] = item.Text; x++; } else if (item.Name.Contains("txtSecLang")) { secLangArrToDb[y] = item.Text; y++; } } } try { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { int p = 0; List <Word> wordResult = (from w in dbConnection.Word where w.TestId == currentTestId select w).ToList(); if (wordResult != null) { foreach (Word item in wordResult) { item.Word1 = mainLangArrToDb[p]; item.Word2 = secLangArrToDb[p]; p++; } dbConnection.SaveChanges(); MessageBox.Show("Successfully saved changes"); } } } catch (EntityException ex) { MessageBox.Show("Could not save the test to database." + ex.Message); } catch (Exception ex) { MessageBox.Show("Error when saving to Database." + ex.Message + ex.InnerException); } finally { Close(); } }