Hi
I was trying to built online quiz. i put all the question in my DB table i was also
able to successfully render those question and there options (Answers).
But when i am using
RadioButtonList1.Items.Selected / SelectedIndex /SelectedValue / SelectedItem ..
it returns -1 or give null exception .
Where am i going wrong ???
Code is as below
public partial class testpage : System.Web.UI.Page
{
public DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("connectionString");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from table ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = "1." + dt.Rows[0]["Question"].ToString();
Label2.Text = "2." + dt.Rows[1]["Question"].ToString();
Label3.Text = "3." + dt.Rows[2]["Question"].ToString();
Label4.Text = "4." + dt.Rows[3]["Question"].ToString();
Label5.Text = "5." + dt.Rows[4]["Question"].ToString();
Label6.Text = "6." + dt.Rows[5]["Question"].ToString();
Label7.Text = "7." + dt.Rows[6]["Question"].ToString();
Label8.Text = "8." + dt.Rows[7]["Question"].ToString();
Label9.Text = "9." + dt.Rows[8]["Question"].ToString();
Label10.Text = "10." + dt.Rows[9]["Question"].ToString();
string[] list1 = { dt.Rows[0]["Answer1"].ToString(), dt.Rows[0]["Answer2"].ToString(), dt.Rows[0]["Answer3"].ToString(), dt.Rows[0]["Answer4"].ToString() };
string[] list2 = { dt.Rows[1]["Answer1"].ToString(), dt.Rows[1]["Answer2"].ToString(), dt.Rows[1]["Answer3"].ToString(), dt.Rows[1]["Answer4"].ToString() };
string[] list3 = { dt.Rows[2]["Answer1"].ToString(), dt.Rows[2]["Answer2"].ToString(), dt.Rows[2]["Answer3"].ToString(), dt.Rows[2]["Answer4"].ToString() };
string[] list4 = { dt.Rows[3]["Answer1"].ToString(), dt.Rows[3]["Answer2"].ToString(), dt.Rows[3]["Answer3"].ToString(), dt.Rows[3]["Answer4"].ToString() };
string[] list5 = { dt.Rows[4]["Answer1"].ToString(), dt.Rows[4]["Answer2"].ToString(), dt.Rows[4]["Answer3"].ToString(), dt.Rows[4]["Answer4"].ToString() };
string[] list6 = { dt.Rows[5]["Answer1"].ToString(), dt.Rows[5]["Answer2"].ToString(), dt.Rows[5]["Answer3"].ToString(), dt.Rows[5]["Answer4"].ToString() };
string[] list7 = { dt.Rows[6]["Answer1"].ToString(), dt.Rows[6]["Answer2"].ToString(), dt.Rows[6]["Answer3"].ToString() };
string[] list8 = { dt.Rows[7]["Answer1"].ToString(), dt.Rows[7]["Answer2"].ToString(), dt.Rows[7]["Answer3"].ToString(), dt.Rows[7]["Answer4"].ToString() };
string[] list9 = { dt.Rows[8]["Answer1"].ToString(), dt.Rows[8]["Answer2"].ToString(), dt.Rows[8]["Answer3"].ToString() };
string[] list10 = { dt.Rows[9]["Answer1"].ToString(), dt.Rows[9]["Answer2"].ToString(), dt.Rows[9]["Answer3"].ToString() };
RadioButtonList1.DataSource = list1;
RadioButtonList1.DataBind();
RadioButtonList2.DataSource = list2;
RadioButtonList2.DataBind();
RadioButtonList3.DataSource = list3;
RadioButtonList3.DataBind();
RadioButtonList4.DataSource = list4;
RadioButtonList4.DataBind();
RadioButtonList5.DataSource = list5;
RadioButtonList5.DataBind();
RadioButtonList6.DataSource = list6;
RadioButtonList6.DataBind();
RadioButtonList7.DataSource = list7;
RadioButtonList7.DataBind();
RadioButtonList8.DataSource = list8;
RadioButtonList8.DataBind();
RadioButtonList9.DataSource = list9;
RadioButtonList9.DataBind();
RadioButtonList10.DataSource = list10;
RadioButtonList10.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string result;
string[] Answer = { dt.Rows[0]["Correct_Answer"].ToString(),
dt.Rows[1]["Correct_Answer"].ToString(),
dt.Rows[2]["Correct_Answer"].ToString(),
dt.Rows[3]["Correct_Answer"].ToString(),
dt.Rows[4]["Correct_Answer"].ToString(),
dt.Rows[5]["Correct_Answer"].ToString(),
dt.Rows[6]["Correct_Answer"].ToString(),
dt.Rows[7]["Correct_Answer"].ToString(),
dt.Rows[8]["Correct_Answer"].ToString(),
dt.Rows[9]["Correct_Answer"].ToString() };
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[1].Selected)
{
result = result + 1;
}
}
Response.Write(result);
}
}
}