Your code is working perfectly fine; you are just using the wrong markup.
Have you looked at the HTML that is rendered from this? It would be helpful so that you can see what is going on.
I see 3 issues with the HTML that would be rendered:
1- All radio buttons have the same
name="options"
. This is giving you the visible problem that you check a button on "Question 4" and your choice for all other questions are erased.
2- You have 4 options and they have IDs of rb1..rb4. These IDs are used by the
label
element and will select the corresponding button when clicked. These same IDs are used in subsequent rows. The DOM (Document Object Model) only allows for an ID to be assigned to 1 element. The net result is that clicking on "Choice 3" for Question 1
could check "Choice 3" on Question 4.
3- The radio buttons have no values assigned to them, so whatever is going to process this form ain't going to know what was checked.
An easy fix to problems 1 & 2 is to use your row iterator
int i
as part of the name/id/for attributes of these elements.
Problem 3 would be fixed by adding the value attribute to the input element
How I would do this it actually utilize another StringBuilder to build a temple before you go through your iterations, and to utilize the
String.Format()
method within the loop to have a cleaner appearance.
Other things changed:
1- I also replaced the DataSet with a DataTable; as you would only need a DataSet if there was more than 1 result set, further cleaning up the code.
2- I used string literals (@) to avoid single quotes.
3- Your original code has each option in it's own TD. Which gives you five rows 1 of 1 column. This is fine, however; it does not align with the "no questions" result of being 1 row of 5 columns. So I removed the TR wrapper on the question and answers
You still have some room for improvement.
1- I stripped out the
color=black on every element. You would only need to do this once, at the table level. You could assign this in the STYLE block on the page.
2- You could close the
table after both IF blocks and remove a line of code
DataTable dt = new DataSet();
StringBuilder htmlTable = new StringBuilder();
string query = "SELECT DISTINCT * FROM Tablename ORDER BY Number ASC";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.Fill(dt);
htmlTable.Append("<table border='0'>");
if (!object.Equals(dt, null))
{
StringBuilder sbQA = new StringBuilder();
sbQA.AppendLine(@"<tr>");
sbQA.AppendLine(@"<td>{0} Question</td></tr>");
sbQA.AppendLine(@"<td><input type=""radio"" id=""rb1_{2}"" name=""options{2}"" value=""A""> A) <label for=""rb1_{2}"">{3}</label></td>");
sbQA.AppendLine(@"<td><input type=""radio"" id=""rb2_{2}"" name=""options{2}"" value=""B""> B) <label for=""rb2_{2}"">{4}</label></td>");
sbQA.AppendLine(@"<td><input type=""radio"" id=""rb3_{2}"" name=""options{2}"" value=""C""> C) <label for=""rb3_{2}"">{5}</label></td>");
sbQA.AppendLine(@"<td><input type=""radio"" id=""rb4_{2}"" name=""options{2}"" value=""D""> D) <label for=""rb4_{2}"">{6}</label></td>");
sbQA.AppendLine(@"</tr>");
string aQuestion = sbQA.ToString();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
htmlTable.AppendFormat(aQuestion, dt.Rows[i]["Number"], i, dt.Rows[i]["ChoiceA"], dt.Rows[i]["ChoiceB"], dt.Rows[i]["ChoiceC"], dt.Rows[i]["ChoiceD"] );
}
htmlTable.Append("</table>");
}
else
{
htmlTable.Append(@"<tr><td style=""text-align:center;"" colspan=""5"">There are No Records.</td></tr>");
htmlTable.Append("</table>");
}
}