i am trying to generate a method in event handler of combo box. I am trying to read data from the database using array so that when i click on the combo box it will link to the textboxes.

I have declared two arrays, one for combo box and another for texboxes

ComboBox[] cbArray = new ComboBox[4];

string[] q = new string[4];

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

// ListQues();

//read the new index value of the combo box being selected

for (int i = 0; i < q.Length; ++i)

{

q[i] = "" + i;

}

cbArray[0].Items.Add(q); 

//use this new index value as an INDEX to retrieve out from the answers from GLOBAL questions array.

}

Dataset class:

public DataSet Ques() //create dataset for ListQue()

{

OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Environment.CurrentDirectory.ToString() + "\\bin\\Debug\\database\\quiz.mdb");

cnn.Open();

string query = "SELECT ques,ans1,ans2,ans3,ans4 FROM questions";

OleDbCommand com = new OleDbCommand(query, cnn);

DataSet ds1 = new DataSet();

OleDbDataAdapter adpt = new OleDbDataAdapter(com);

adpt.Fill(ds1, "questions");

cnn.Close();

return ds1;

}
public class questions

{

public string answer1;

public string answer2;

public string answer3;

public string answer4;

}

ListQues() class:

private void ListQues()

{

DataSet ds1 = new DataSet();

ds1 = Ques();

for (int i = 0; i <= ds1.Tables["questions"].Rows.Count-1; i++)

{

questions q = new questions();

q.answer1 = ds1.Tables["questions"].Rows[i]["ans1"].ToString();

q.answer2 = ds1.Tables["questions"].Rows[i]["ans2"].ToString();

q.answer3 = ds1.Tables["questions"].Rows[i]["ans3"].ToString();

q.answer4 = ds1.Tables["questions"].Rows[i]["ans4"].ToString();

txtAns1.Text = q.ToString();

txtAns2.Text = q.ToString();

txtAns3.Text = q.ToString();

txtAns4.Text = q.ToString();

comboBox1.DataSource = ds1.Tables[0];

comboBox1.DisplayMember = "ques";

}

}

Thanks in advance.

I have attached screenshot of the program which I am referring to.

I want to select another question, but the answers reflected must be matched to the correct question which is saved in the database.

Forgive stupid question - why not just link the text boxes to a dataset and have the dataset questioned on which one you select from the combo.. why reinvent the wheel?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.