Save Selected Data of Radio Button List in ASP.Net

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.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 211px;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             width: 224px;  
  16.         }  
  17.         .style3  
  18.         {  
  19.             width: 224px;  
  20.             font-weight: bold;  
  21.             text-decoration: underline;  
  22.         }  
  23.         .style4  
  24.         {  
  25.             width: 211px;  
  26.             font-weight: bold;  
  27.             text-decoration: underline;  
  28.         }  
  29.     </style>  
  30. </head>  
  31. <body>  
  32.     <form id="form1" runat="server">  
  33.     <div>  
  34.       
  35.     </div>  
  36.     <table style="width:100%;">  
  37.         <tr>  
  38.             <td class="style4">  
  39.                 </td>  
  40.             <td class="style3">  
  41.                 Please Answer these Questions</td>  
  42.             <td>  
  43.                  </td>  
  44.         </tr>  
  45.         <tr>  
  46.             <td class="style1">  
  47.                  </td>  
  48.             <td class="style2">  
  49.                  </td>  
  50.             <td>  
  51.                  </td>  
  52.         </tr>  
  53.         <tr>  
  54.             <td class="style1">  
  55.                 <asp:Label ID="Label7" runat="server"   
  56.                     Text="Is Earth is the only planet in the universe??"></asp:Label>  
  57.             </td>  
  58.             <td class="style2">  
  59.                 <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataTextField="ans"   
  60.                     DataValueField="ans">  
  61.                     <asp:ListItem>True</asp:ListItem>  
  62.                     <asp:ListItem>False</asp:ListItem>  
  63.                 </asp:RadioButtonList>  
  64.             </td>  
  65.             <td>  
  66.                  </td>  
  67.         </tr>  
  68.         <tr>  
  69.             <td class="style1">  
  70.                 <asp:Label ID="Label5" runat="server" Text="Is Moon is our Natural Satellite?"></asp:Label>  
  71.             </td>  
  72.             <td class="style2">  
  73.                 <asp:RadioButtonList ID="RadioButtonList2" runat="server" DataTextField="ans1"   
  74.                     DataValueField="ans1">  
  75.                     <asp:ListItem>True</asp:ListItem>  
  76.                     <asp:ListItem>False</asp:ListItem>  
  77.                 </asp:RadioButtonList>  
  78.             </td>  
  79.             <td>  
  80.                  </td>  
  81.         </tr>  
  82.         <tr>  
  83.             <td class="style1">  
  84.                 <asp:Label ID="Label6" runat="server"   
  85.                     Text="Earth is having Rings like Saturn have ?"></asp:Label>  
  86.             </td>  
  87.             <td class="style2">  
  88.                 <asp:RadioButtonList ID="RadioButtonList3" runat="server" DataTextField="ans2"   
  89.                     DataValueField="ans2">  
  90.                     <asp:ListItem>True</asp:ListItem>  
  91.                     <asp:ListItem>False</asp:ListItem>  
  92.                 </asp:RadioButtonList>  
  93.             </td>  
  94.             <td>  
  95.                  </td>  
  96.         </tr>  
  97.         <tr>  
  98.             <td class="style1">  
  99.                  </td>  
  100.             <td class="style2">  
  101.                  </td>  
  102.             <td>  
  103.                  </td>  
  104.         </tr>  
  105.         <tr>  
  106.             <td class="style1">  
  107.                  </td>  
  108.             <td class="style2">  
  109.     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit your answer" />  
  110.             </td>  
  111.             <td>  
  112.                 <asp:Label ID="lbmsg" runat="server"></asp:Label>  
  113.             </td>  
  114.         </tr>  
  115.         <tr>  
  116.             <td class="style1">  
  117.                  </td>  
  118.             <td class="style2">  
  119.                  </td>  
  120.             <td>  
  121.                  </td>  
  122.         </tr>  
  123.     </table>  
  124.     </form>  
  125. </body>  
  126. </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.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.   
  15.     }  
  16.     protected void Button1_Click(object sender, EventArgs e)  
  17.     {  
  18.         SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  19.         SqlCommand cmd = new SqlCommand("insert into tbl_data (ans,ans1,ans2) values (@ans,@ans1,@ans2)", con);  
  20.         cmd.Parameters.AddWithValue("ans", RadioButtonList1.SelectedItem.Text);  
  21.         cmd.Parameters.AddWithValue("ans1", RadioButtonList2.SelectedItem.Text);  
  22.         cmd.Parameters.AddWithValue("ans2", RadioButtonList3.SelectedItem.Text);  
  23.           
  24.         con.Open();  
  25.         int i = cmd.ExecuteNonQuery();  
  26.         con.Close();  
  27.   
  28.         if (i != 0)  
  29.         {  
  30.             lbmsg.Text = "Your Answer Submitted Succesfully";  
  31.             lbmsg.ForeColor = System.Drawing.Color.ForestGreen;  
  32.         }  
  33.         else  
  34.         {  
  35.            lbmsg.Text = "Some Problem Occured";  
  36.             lbmsg.ForeColor = System.Drawing.Color.Red;              
  37.           
  38.         }  
  39.     }  
  40. }  
Output Chamber

submmit

Output

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


Similar Articles