We make some true or false questions so the user will provide their answer and on a button click the answer will be saved to a database.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an empty website, provide a suitable name (RadioButtonList_demo).

Step 2

In Solution Explorer you get your empty website, add a web form and SQL Database as in the following.

For Web Form:

RadioButtonList_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it RadioButtonList_demo.aspx.

For SQL Server database:

RadioButtonList_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add the database inside the App_Data_folder.)

Database Chamber

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this:

table design
This table is fore saving the data of the radio button list, I mean in this table we will get the user's answer of true and false questions.

Design Chamber

Step 4

Open your RadioButtonList_demo.aspx file from Solution Explorer and start the design of you're application.

Here is the code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. width: 211px;
  10. }
  11. .style2
  12. {
  13. width: 224px;
  14. }
  15. .style3
  16. {
  17. width: 224px;
  18. font-weight: bold;
  19. text-decoration: underline;
  20. }
  21. .style4
  22. {
  23. width: 211px;
  24. font-weight: bold;
  25. text-decoration: underline;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <form id="form1" runat="server">
  31. <div>
  32. </div>
  33. <table style="width:100%;">
  34. <tr>
  35. <td class="style4">
  36. </td>
  37. <td class="style3">
  38. Please Answer these Questions</td>
  39. <td>
  40. </td>
  41. </tr>
  42. <tr>
  43. <td class="style1">
  44. </td>
  45. <td class="style2">
  46. </td>
  47. <td>
  48. </td>
  49. </tr>
  50. <tr>
  51. <td class="style1">
  52. <asp:Label ID="Label7" runat="server"
  53. Text="Is Earth is the only planet in the universe??"></asp:Label>
  54. </td>
  55. <td class="style2">
  56. <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataTextField="ans"
  57. DataValueField="ans">
  58. <asp:ListItem>True</asp:ListItem>
  59. <asp:ListItem>False</asp:ListItem>
  60. </asp:RadioButtonList>
  61. </td>
  62. <td>
  63. </td>
  64. </tr>
  65. <tr>
  66. <td class="style1">
  67. <asp:Label ID="Label5" runat="server" Text="Is Moon is our Natural Satellite?"></asp:Label>
  68. </td>
  69. <td class="style2">
  70. <asp:RadioButtonList ID="RadioButtonList2" runat="server" DataTextField="ans1"
  71. DataValueField="ans1">
  72. <asp:ListItem>True</asp:ListItem>
  73. <asp:ListItem>False</asp:ListItem>
  74. </asp:RadioButtonList>
  75. </td>
  76. <td>
  77. </td>
  78. </tr>
  79. <tr>
  80. <td class="style1">
  81. <asp:Label ID="Label6" runat="server"
  82. Text="Earth is having Rings like Saturn have ?"></asp:Label>
  83. </td>
  84. <td class="style2">
  85. <asp:RadioButtonList ID="RadioButtonList3" runat="server" DataTextField="ans2"
  86. DataValueField="ans2">
  87. <asp:ListItem>True</asp:ListItem>
  88. <asp:ListItem>False</asp:ListItem>
  89. </asp:RadioButtonList>
  90. </td>
  91. <td>
  92. </td>
  93. </tr>
  94. <tr>
  95. <td class="style1">
  96. </td>
  97. <td class="style2">
  98. </td>
  99. <td>
  100. </td>
  101. </tr>
  102. <tr>
  103. <td class="style1">
  104. </td>
  105. <td class="style2">
  106. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit your answer" />
  107. </td>
  108. <td>
  109. <asp:Label ID="lbmsg" runat="server"></asp:Label>
  110. </td>
  111. </tr>
  112. <tr>
  113. <td class="style1">
  114. </td>
  115. <td class="style2">
  116. </td>
  117. <td>
  118. </td>
  119. </tr>
  120. </table>
  121. </form>
  122. </body>
  123. </html>
This is how your design looks:

design

Code Chamber

Finally open your RadioButtonList_demo.aspx.cs file to write the code, for making the list box like we assume.

Here is the code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  17. SqlCommand cmd = new SqlCommand("insert into tbl_data (ans,ans1,ans2) values (@ans,@ans1,@ans2)", con);
  18. cmd.Parameters.AddWithValue("ans", RadioButtonList1.SelectedItem.Text);
  19. cmd.Parameters.AddWithValue("ans1", RadioButtonList2.SelectedItem.Text);
  20. cmd.Parameters.AddWithValue("ans2", RadioButtonList3.SelectedItem.Text);
  21. con.Open();
  22. int i = cmd.ExecuteNonQuery();
  23. con.Close();
  24. if (i != 0)
  25. {
  26. lbmsg.Text = "Your Answer Submitted Succesfully";
  27. lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
  28. }
  29. else
  30. {
  31. lbmsg.Text = "Some Problem Occured";
  32. lbmsg.ForeColor = System.Drawing.Color.Red;
  33. }
  34. }
  35. }
Output Chamber

submmit

Output

I hope you like this. Thank you for Reading. Have a good day.