lundi 16 avril 2018

Checkbox value stored and inserted multiple times

I have a program where i need to register questions. A question can have 1 or multiple right answers, as checked in the method checkTaskType(). All ints are declared in the start of the code. Code runs fine and works, problem is that if i want to register multiple questions without closing the form, the value from previouse checkbox will be registered as a correct answer on the next question.

    //Method for insert to DB
    private void InsertQAData()
    {
        using (var con = new MySqlConnection(_connectionString))
        {
            con.Open();
            MySqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText =
                @"INSERT INTO oppgave (oppgavetekst, alt_1, alt_2, alt_3, alt_4, alt_5, illustrasjon_link, oppgave_type, alt_1_korrekt, alt_2_korrekt, alt_3_korrekt, alt_4_korrekt, alt_5_korrekt)
            VALUES (@taskText, @alt_1, @alt_2, @alt_3, @alt_4, @alt_5, @illustration_link, @taskType, @corrAlt1, @corrAlt2, @corrAlt3, @corrAlt4, @corrAlt5);";
            cmd.Parameters.AddWithValue("@taskText", txtQuestion.Text);
            cmd.Parameters.AddWithValue("@alt_1", txtAlt_1.Text);
            cmd.Parameters.AddWithValue("@alt_2", txtAlt_2.Text);
            cmd.Parameters.AddWithValue("@alt_3", txtAlt_3.Text);
            cmd.Parameters.AddWithValue("@alt_4", txtAlt_4.Text);
            cmd.Parameters.AddWithValue("@alt_5", txtAlt_5.Text);
            cmd.Parameters.AddWithValue("@illustration_link", txtLink.Text);
            cmd.Parameters.AddWithValue("@taskType", taskType);
            cmd.Parameters.AddWithValue("@corrAlt1", corrAlt1);
            cmd.Parameters.AddWithValue("@corrAlt2", corrAlt2);
            cmd.Parameters.AddWithValue("@corrAlt3", corrAlt3);
            cmd.Parameters.AddWithValue("@corrAlt4", corrAlt4);
            cmd.Parameters.AddWithValue("@corrAlt5", corrAlt5);
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
            lblInserted.Show();
      }

    // Method for checking if a question has 1 or multiple answers
    private void checkTaskType()
    {
        if (checkBox1.Checked)
        {
            count += 1;
            corrAlt1 = 1;
        }
        if (checkBox2.Checked)
        {
            count += 1;
            corrAlt2 = 1;
        }
        if (checkBox3.Checked)
        {
            count += 1;
            corrAlt3 = 1;
        }
        if (checkBox4.Checked)
        {
            count += 1;
            corrAlt4 = 1;
        }
        if (checkBox5.Checked)
        {
            count += 1;
            corrAlt5 = 1;
        }
        if (count == 1)
        {
            taskType = "ettAlternativ";
        }
        if (count > 1)
        {
            taskType = "flereAlternativer";
        }
    }




Aucun commentaire:

Enregistrer un commentaire