private void loadNewQuestion()
        {
            //wersja z Sql server
            //SqlConnection conn = new SqlConnection("Server=DESKTOP-BO91STR\\SQLEXPRESS;Database=Quiz;Integrated Security=true");
            //conn.Open();

            //wersja z azure

            /*SqlConnection conn = null;
             * try
             * {
             *  var cb = new SqlConnectionStringBuilder();
             *  cb.DataSource = "serv2703.database.windows.net";
             *  cb.PersistSecurityInfo = false;
             *  cb.MultipleActiveResultSets = false;
             *  cb.Encrypt = true;
             *  cb.TrustServerCertificate = false;
             *  cb.ConnectTimeout = 30;
             *  cb.UserID = "adam";
             *  cb.Password = "******";
             *  cb.InitialCatalog = "Quiz";
             *
             *  conn = new SqlConnection(cb.ConnectionString);
             *  conn.Open();
             * }
             * catch (SqlException e)
             * {
             *  Console.WriteLine(e.ToString());
             * }*/

            //część uniwersalna dla sql
            // SqlCommand cmd = new SqlCommand(
            //     "Select q.Description from Questions2 q where q.Topic='"+scenario+"' and q.Difficulty="+currentQuestion+"", conn);
            // SqlDataReader reader = cmd.ExecuteReader();
            // while (reader.Read())
            // {
            //     Quest.Text = reader.GetString(0);
            //     Question.Text = "Question" + currentQuestion;

            // }
            // cmd=new SqlCommand(
            //     "select a.[Description] , a.IsCorrect from Questions2 q join Answers2 a on a.QuestionId = q.QuestionId where q.Topic='" + scenario + "' and q.Difficulty =" + currentQuestion + "", conn);
            // reader.Close();
            //reader = cmd.ExecuteReader();
            // int currentans = 0;
            // while (reader.Read())
            // {
            //     answers.ElementAt(currentans).Text=reader.GetString(0);
            //     if (reader.GetBoolean(1))
            //     {
            //         correctAns = answers.ElementAt(currentans);
            //     }
            //     currentans++;
            // }
            // Shuffle(answers);
            // ansA = answers.ElementAt(0);
            // ansB = answers.ElementAt(1);
            // ansC = answers.ElementAt(2);
            // ansD = answers.ElementAt(3);
            // foreach(Button b in answers)
            // {
            //     if (b.Equals(correctAns))
            //     {
            //         correctNumb = answers.IndexOf(b);
            //     }
            // }
            // reader.Close();
            // conn.Close();
            toJson          tJ = new toJson();
            List <Question> lq = new List <Question>();
            List <Answer>   la = new List <Answer>();

            lq = tJ.serializeQuestions();
            la = tJ.serializeAnswers();
            Question currQ = null;

            foreach (Question q in lq)
            {
                if (q.Topic == scenario && q.Difficulty == currentQuestion)
                {
                    currQ         = q;
                    Quest.Text    = q.Description;
                    Question.Text = "Question" + currentQuestion;
                    break;
                }
            }
            int currentans = 0;

            foreach (Answer a in la)
            {
                if (a.QuestionId == currQ.QuestionId)
                {
                    answers.ElementAt(currentans).Text = a.Description;
                    if (a.IsCorrect == 1)
                    {
                        correctAns = answers.ElementAt(currentans);
                    }
                    currentans++;
                }
            }
            Shuffle(answers);
            ansA = answers.ElementAt(0);
            ansB = answers.ElementAt(1);
            ansC = answers.ElementAt(2);
            ansD = answers.ElementAt(3);
            foreach (Button b in answers)
            {
                if (b.Equals(correctAns))
                {
                    correctNumb = answers.IndexOf(b);
                }
            }
        }
Exemple #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            //get last QuestionId and AnswerId
            toJson          tJ              = new toJson();
            List <Question> lq              = new List <Question>();
            List <Answer>   la              = new List <Answer>();
            int             biggestIndex    = 0;
            int             currentAnsIndex = 0;

            foreach (Question qq in lq)
            {
                if (biggestIndex <= qq.QuestionId)
                {
                    biggestIndex = qq.QuestionId;
                }
            }
            foreach (Answer a in la)
            {
                if (currentAnsIndex <= a.AnswerId)
                {
                    currentAnsIndex = a.AnswerId;
                }
            }

            Question q = new Question();

            q.Description = txtQuestion.Text;
            q.Difficulty  = Int32.Parse(cmbDifficulty.Text);
            q.QuestionId  = biggestIndex + 1;
            q.Topic       = cmbScenario.Text;
            lq.Add(q);

            // Update json data string
            var question = JsonConvert.SerializeObject(lq);

            System.IO.File.WriteAllText("C:\\Users\\Administrator\\source\\repos\\QUIZ\\QUIZ\\questions.json", question);

            List <string> answers    = new List <string>();
            List <Answer> newAnswers = new List <Answer>();

            answers.Add(txtAnsA.Text);
            answers.Add(txtAnsB.Text);
            answers.Add(txtAnsC.Text);
            answers.Add(txtAnsD.Text);
            foreach (string s in answers)
            {
                currentAnsIndex++;
                Answer a = new Answer();
                a.Description = s;
                a.QuestionId  = q.QuestionId;
                a.IsCorrect   = 0;
                a.AnswerId    = currentAnsIndex;
                newAnswers.Add(a);
            }
            switch (cmbCorrect.Text)
            {
            case "AnsA":
                newAnswers[0].IsCorrect = 1;
                break;

            case "AnsB":
                newAnswers[1].IsCorrect = 1;
                break;

            case "AnsC":
                newAnswers[2].IsCorrect = 1;
                break;

            case "AnsD":
                newAnswers[3].IsCorrect = 1;
                break;
            }
            la.AddRange(newAnswers);
            // Update json data string
            var ans = JsonConvert.SerializeObject(la);

            System.IO.File.WriteAllText("C:\\Users\\Administrator\\source\\repos\\QUIZ\\QUIZ\\ans.json", ans);
        }