0

*sorry if the subject is not good but i didn't know what to right in it.

I have a code to connect Access Database with my C# Windows Forms program. The program is a school test .. as i get the question and the multiple answers from the database.I have 7 questions written in that access database. My problem is that when i run the code, I only get the last question (question number 7) but I want to show another question from another row in the database (the question I want).

            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\Release\AppDB.accdb");
            con.Open();

            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1", con);
            da.Fill(dt);

            foreach (DataRow myRow in dt.Rows)
            {
                label1.Text = "Question " + myRow[0] + " / " + myRow[1].ToString();
                radioButton1.Text = myRow[2].ToString();
                radioButton2.Text = myRow[3].ToString();
                radioButton3.Text = myRow[4].ToString();
                radioButton4.Text = myRow[5].ToString();

                label3.Text = myRow[6].ToString();
            }

            con.Close();
1
  • Each time you loop through, you are replacing the old value which is assigned to the controls. So at the end of the loop execution the final row value will remain in the controls.(As per your code). Commented Sep 26, 2013 at 11:46

1 Answer 1

1

Your problem is that you are looping through each row and overwriting the values in your radio buttons each time - meaning you'll always get the last one. You need to change your select statement to:

OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1 Where <field> = <value>", con);

Replace field and value with the name of the column and the value you wish to check respectively.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.