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:
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:
This is how your design looks:
Code ChamberFinally open your RadioButtonList_demo.aspx.cs file to write the code, for making the list box like we assume.
Here is the code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("insert into tbl_data (ans,ans1,ans2) values (@ans,@ans1,@ans2)", con);
- cmd.Parameters.AddWithValue("ans", RadioButtonList1.SelectedItem.Text);
- cmd.Parameters.AddWithValue("ans1", RadioButtonList2.SelectedItem.Text);
- cmd.Parameters.AddWithValue("ans2", RadioButtonList3.SelectedItem.Text);
-
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
-
- if (i != 0)
- {
- lbmsg.Text = "Your Answer Submitted Succesfully";
- lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
- }
- else
- {
- lbmsg.Text = "Some Problem Occured";
- lbmsg.ForeColor = System.Drawing.Color.Red;
-
- }
- }
- }
Output ChamberI hope you like this. Thank you for Reading. Have a good day.