Пример #1
0
        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);
            }
        }
Пример #2
0
        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();
            }
        }
Пример #3
0
 public void onLoad()
 {
     try
     {
         using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
         {
             cbTestIdAndName.DataSource    = dbConnection.Test.ToList();
             cbTestIdAndName.DisplayMember = "TestId";
             cbTestIdAndName.Invalidate();
         }
     }
     catch (EntityException ex)
     {
         MessageBox.Show("Could not populate the dropdown list." + ex.Message);
     }
     catch (Exception ex)
     {
         MessageBox.Show("General error when populating the dropdown list." + ex.Message + ex.InnerException);
     }
 }
Пример #4
0
        private void CreateTest_OnLoad()
        {
            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    cBMainLang.DataSource    = dbConnection.Languages.ToList();
                    cBMainLang.DisplayMember = "MainLang";
                    cBSecLang.DataSource     = dbConnection.Languages.ToList();
                    cBSecLang.DisplayMember  = "SecLang";
                    cBMainLang.Invalidate();
                    cBSecLang.Invalidate();
                }
            }

            catch (EntityException ex)
            {
                MessageBox.Show("Could not populate the dropdown list." + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("General error when populating the dropdown list." + ex.Message + ex.InnerException);
            }
        }
Пример #5
0
        private void btnLoadTest_Click(object sender, EventArgs e)
        {
            int cb;

            int.TryParse(cbTestIdAndName.SelectedValue.ToString(), out cb);

            using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
            {
                var selectedTest = (from t in dbConnection.Test
                                    where t.Id == cb
                                    select new {
                    t.Id,
                    t.Name,
                    t.MainLang,
                    t.SecLang
                }).FirstOrDefault();

                //Gets the id of the recently saved test to be able to perform LINQ-questions below.
                currentTestId = selectedTest.Id;

                mainLang = (from l in dbConnection.Languages
                            where l.Id == selectedTest.MainLang
                            select l.Language).FirstOrDefault();


                secLang = (from l in dbConnection.Languages
                           where l.Id == selectedTest.SecLang
                           select l.Language).FirstOrDefault();

                var word = (from w in dbConnection.Word
                            where w.TestId == currentTestId
                            select new
                {
                    w.Id,
                    w.TestId,
                    w.Word1,
                    w.Word2
                });

                if (selectedTest != null && word != null)
                {
                    numberOfWords = word.Count();

                    int a = 0;
                    mainLangArr = new string[numberOfWords];
                    secLangArr  = new string[numberOfWords];

                    //Debug.WriteLine(numberOfWords.ToString(), mainLangArr.Count(), secLangArr.Count());
                    //Debug.WriteLine(selectedTest.Name.ToString(), cb.ToString());
                    foreach (var item in word)
                    {
                        mainLangArr[a] = item.Word1;
                        secLangArr[a]  = item.Word2;
                        a++;
                        //Debug.WriteLine(item.TestId.ToString() + item.Word1 + item.Word2);
                    }

                    loadPanel();
                }
            }
        }
Пример #6
0
        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();
            }
        }