Building a Questionaire Application with one question and four options using ASP.Net

sara43
 
on Feb 04, 2013 04:13 AM
3027 Views

sir, 

i have a task, online test. 15 questions stored in database , and also answers.

it has 4 options, a, b, c, d. for a particular question how to fil values in respective options from database.?

and also how to move to next questions, by clicking next button

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
on Feb 06, 2013 02:39 AM
on Feb 06, 2013 03:18 AM

Ok you can try this

 

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;
using System.Configuration;

public partial class RadioButtonBinding : System.Web.UI.Page
{
    protected int QuestionNo
    {
        get
        {
            return Session["QuestionNo"] != null ? (int)Session["QuestionNo"] : 1;
        }
        set
        {
            Session["QuestionNo"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dataTable = new DataTable();
            dataTable = GetData("SELECT [Q.NO],[QUESTION],[A],[B],[C],[D],[ANS] FROM ONLINETEST WHERE [Q.NO] = " + this.QuestionNo + "");
            rblOptions.AppendDataBoundItems = true;
            if (dataTable.Rows.Count > 0)
            {
                lblQuestion.Text = dataTable.Rows[0]["Q.NO"].ToString() + ". " + dataTable.Rows[0]["QUESTION"].ToString();
                rblOptions.Items.Add(new ListItem { Text = "a." + dataTable.Rows[0]["A"].ToString(), Value = dataTable.Rows[0]["A"].ToString() });
                rblOptions.Items.Add(new ListItem { Text = "b." + dataTable.Rows[0]["B"].ToString(), Value = dataTable.Rows[0]["B"].ToString() });
                rblOptions.Items.Add(new ListItem { Text = "c." + dataTable.Rows[0]["C"].ToString(), Value = dataTable.Rows[0]["C"].ToString() });
                rblOptions.Items.Add(new ListItem { Text = "d." + dataTable.Rows[0]["D"].ToString(), Value = dataTable.Rows[0]["D"].ToString() });
            }
        }
    }

    protected void OnNextClicked(object sender, EventArgs e)
    {
        //Here if you want you can write the code to save the answer selected by user in the database

        this.QuestionNo++;
        //Here you have to again redirect to the same page
        Response.Redirect("~/RadioButtonBinding.aspx");
    }

    private DataTable GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}