I am trying to get my program to read a value from a particular cell in my Access database so that I can assign that value to the text property of a radio button. I am however getting an error I can't figure out the problem to. This is my code:
private void WindowsAnalysisQuiz_Load(object sender, EventArgs e)
{
//declare connection string using windows security
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\WindowsAnalysisQuiz.accdb";
//declare Connection, command and other related objects
OleDbConnection conGet = new OleDbConnection(cnString);
OleDbCommand cmdGet = new OleDbCommand();
try
{
//open connection
conGet.Open();
cmdGet.CommandType = CommandType.Text;
cmdGet.Connection = conGet;
//cmdGet.CommansText = "SELECT
cmdGet.CommandText = "SELECT Question FROM WindowsAnalysisQuiz ORDER BY rnd()";
OleDbDataReader reader = cmdGet.ExecuteReader();
reader.Read();
label1.Text = reader["Question"].ToString();
radioButton.Text = cnString["Quiz"].Rows[0]["Correct Answer"].ToString();
radioButton1.Text = reader["Answer2"].ToString();
radioButton2.Text = reader["Answer3"].ToString();
radioButton3.Text = reader["Answer4"].ToString();
conGet.Close();
}
}
I am having the problem with the line beginnin开发者_Go百科g with radioButton.Text. Apparently it has some invalid arguments and argument 1 cannot convert from string to int
It looks like you are using several different RadioButtons, when you might really want a RadioButtonList.
If you have a RadioButtonList, it is easy to bind something to it, in essence create a list (from your database or whatever) and then bind this to the radio button list..something like
var de = new List<string>();
de.Add("1");
de.Add("2");
de.Add("3");
RadioButtonList1.DataSource = de;
RadioButtonList1.DataBind();
Shouldn't your code be something like the following:
radioButton.Text = reader["Correct Answer"].ToString();
And change your select statement to:
cmdGet.CommandText = "SELECT Question, [Correct Answer], Answer2, Answer3, Answer4 FROM WindowsAnalysisQuiz ORDER BY rnd()";
精彩评论