Hi,
in my application i am using the following code to display
requested number of Questions and it's Answers in a page. after
clicking Finish button i have to save the selected options into Sql
database table
Result(idTable,idQuestion,SelectedOption,Correct,Incorrect,Marked)
my code is:
static int noq;
protected void Page_Load(object sender, EventArgs e)
{
noq = Convert.ToInt16(Session["Noq"]);
ReadQuestionsIntoTable();
}
DataSet ds1 = new DataSet("questionsds");
int NumberOfQuestions = 0;
public void ReadQuestionsIntoTable()
{
SqlConnection cn=new SqlConnection("user id=sa;
password=sa123; database=questionbank;data source=MERCURY-987BECC");
SqlDataAdapter da1 = new SqlDataAdapter("Select * From
Question Where idquestion IN(Select idQuestion From Category_Details
Where idSubCategory IN(Select idSubCategory From SubCategory Where
SubCategoryName='" + Session["SelectedSubCategory"].ToString()+ "'))",
cn);
SqlDataAdapter da2 = new SqlDataAdapter("Select * From
Answer_Detail Where idquestion IN(Select idQuestion From
Category_Details Where idSubCategory IN(Select idSubCategory From
SubCategory Where SubCategoryName='" +
Session["SelectedSubCategory"].ToString() + "'))", cn);
// Fill the questions and choices tables in memory
da1.Fill(ds1,"Questions");
da2.Fill(ds1,"Choices");
DataTable QuestionsTable=ds1.Tables["Questions"];
DataTable ChoicesTable=ds1.Tables["Choices"];
// create a data relation between the Questions and Choices
Tables
DataRelation QALink=new
DataRelation("QuestionLink",QuestionsTable.Columns["idquestion"],ChoicesTable.Columns["idquestion"]);
QuestionsTable.ChildRelations.Add(QALink);
// go through every row in the questions table
// and place each question in the Table Web Control
for (int i = 0; i < noq;i++ )
{
DataRow dr = QuestionsTable.Rows[i];
// create a row for the question and read it from the
database
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
TableCell aCell = new TableCell();
// get the text for the question and stick it in the cell
aCell.Text = dr["Qname"].ToString();
tr.Cells.Add(aCell);
string[] AnswerArray = new string[50];
AnswerArray[NumberOfQuestions] = dr["Answer"].ToString();
// create a row for the choices and read from the database
int count = 0;
// go through the child rows of the question table
foreach (DataRow choiceRow in dr.GetChildRows(QALink))
{
TableRow tr2 = new TableRow();
Table1.Rows.Add(tr2);
// create a cell for the choice
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;
// align the choices on the left
aCell3.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(aCell3);
// create a radio button in the cell
RadioButton rb = new RadioButton();
// assign the radio button to Group + QuestionID
rb.GroupName = "Group" +
choiceRow["idquestion"].ToString();
// Assign the choice to the radio button
rb.Text = choiceRow["options"].ToString();
// Assign the radio button id corresponding to the
choice and question
rb.ID ="Radio" + NumberOfQuestions.ToString()
+Convert.ToChar(count + 65);
Response.Write((rb.ID).ToString());
rb.Visible = true;
// add the radio button to the cell
aCell3.Controls.Add(rb);
count++;
}
// add a table row between each question
// as a spacer
TableRow spacer = new TableRow();
spacer.Height = 30;
TableCell spacerCell = new TableCell();
spacerCell.Height = 30;
spacer.Cells.Add(spacerCell);
Table1.Rows.Add(spacer);
NumberOfQuestions++;
}
}
after clicking button, how should i save the selected option into
database table(Result table). because during above code, when number
of question is 10(i.e.. noq=10), then 10 questions will be displayed
along with options.
the generated radio button id's are
Radio0A,Radio0B,Radio0C,Radio0d,radio1a,Radio1B............................................Radio9D
how should i use these id's to save selected option into database..if
number of question is changed the generated id's also change..