我正在尝试让我的程序从Access数据库中的特定单元格读取值,以便我可以将该值分配给单选按钮的text属性。然而,我收到一个错误,我无法弄清楚问题。这是我的代码:
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();
}
}
我遇到了以radioButton.Text开头的行的问题。显然它有一些无效的参数,参数1不能从字符串转换为int
答案 0 :(得分:0)
您的代码不应该类似于以下内容:
radioButton.Text = reader["Correct Answer"].ToString();
将您的select语句更改为:
cmdGet.CommandText = "SELECT Question, [Correct Answer], Answer2, Answer3, Answer4 FROM WindowsAnalysisQuiz ORDER BY rnd()";
答案 1 :(得分:0)
看起来你正在使用几个不同的RadioButton,当你真的想要一个RadioButtonList时。
如果你有一个RadioButtonList,很容易绑定它,实质上创建一个列表(从你的数据库或其他),然后将其绑定到单选按钮列表..像
var de = new List<string>();
de.Add("1");
de.Add("2");
de.Add("3");
RadioButtonList1.DataSource = de;
RadioButtonList1.DataBind();