2011-06-30 8 views
0

私のプログラムが、その値をラジオボタンのテキストプロパティに割り当てることができるように、Accessデータベース内の特定のセルから値を読み取ろうとしています。私はしかし、私は問題を把握することはできませんエラーが発生しています。これは私のコードです:データベースからのラジオボタンの値

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が

+0

のようなラジオボタンlist..somethingにこれを結合し、それに何かをバインドするのは簡単ですインデクサを使用して文字列 'cnString'にアクセスしようとしているため、エラーがスローされます。これは整数インデックスのみを受け入れます。 – Zebi

答えて

0

RadioButtonListが本当に必要な場合は、いくつかの異なるRadioButtonを使用しているようです。

あなたはRadioButtonListのを持っている場合、本質的に(データベースまたは任意の)リストを作成し、

 var de = new List<string>(); 
     de.Add("1"); 
     de.Add("2"); 
     de.Add("3"); 

     RadioButtonList1.DataSource = de; 
     RadioButtonList1.DataBind(); 
0

をintに文字列から変換することはできませんがあります。

radioButton.Text = reader["Correct Answer"].ToString(); 

とにSELECT文を変更します。

cmdGet.CommandText = "SELECT Question, [Correct Answer], Answer2, Answer3, Answer4 FROM WindowsAnalysisQuiz ORDER BY rnd()"; 
+0

これは、「正解」というメッセージボックスです。第1列のセルの実際の値、この場合は3行目が実際のラジオボタンの隣に表示されます。 – user612041

関連する問題