简体   繁体   中英

C# - Populating a List with data from a database

I have a List (listOfQuestionIDs) stored in ViewState of 10 different numbers and an int counter (questionCounter) stored in ViewState which I intend to use to access the different numbers in the first list.

However, as I increment the value of ViewState["questionCounter"] by 1 the contents of listOfAnswerIDs and listOfAnswers remain exactly the same in the code below. Why is this?

protected void getAnswers()
{
    string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySqlConnection conn = new MySqlConnection(connStr);
    MySqlDataReader reader;

    List<int> listOfQuestionIDs = (List<int>)(ViewState["listOfQuestionIDs"]);
    int questionCounter = Convert.ToInt32(ViewState["questionCounter"]);

    List<string> listOfAnswerIDs = new List<string>();
    List<string> listOfAnswers = new List<string>();
    List<string> listOfCorrectAnswerIDs = new List<string>();

    try
    {
        conn.Open();

        string cmdText = "SELECT * FROM answers WHERE question_id=@QuestionID";
        MySqlCommand cmd = new MySqlCommand(cmdText, conn);
        cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
        cmd.Parameters["@QuestionID"].Value = Convert.ToInt32(listOfQuestionIDs[questionCounter]);

        reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            listOfAnswerIDs.Add(reader["answer_id"].ToString());
            listOfAnswers.Add(reader["answer"].ToString());

            if (reader["correct"].ToString().Equals("Y"))
            {
                listOfCorrectAnswerIDs.Add(reader["answer_id"].ToString());
            }
        }

        for (int i = 0; i < listOfAnswerIDs.Count; i++)
        {
            lblTitle.Text += listOfAnswerIDs[i].ToString();
        }

        reader.Close();

        populateAnswers(listOfAnswerIDs.Count, listOfAnswers, listOfAnswerIDs);
    }
    catch
    {
        lblError.Text = "Database connection error - failed to read records.";
    }
    finally
    {
        conn.Close();
    }

    ViewState["listOfQuestionIDs"] = listOfQuestionIDs;
    ViewState["listOfCorrectAnswerIDs"] = listOfCorrectAnswerIDs;
}

Here's where I increment the questionCounter:

protected void btnNext_Click(object sender, EventArgs e)
{
    .........
    getNextQuestion();
}

protected void getNextQuestion()
{
    int questionCounter = Convert.ToInt32(ViewState["QuestionCounter"]);
    ..........
    getAnswers();
    ................
    questionCounter += 1;
    ViewState["QuestionCounter"] = questionCounter;
}

ViewState keys are case sensitive. In getAnswers() you use "questionCounter" and in getNextQuestion() you use "QuestionCounter". Pick one and keep it consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM