I am trying to create some thing like MCQ(multiple choice question). I am able to display my question on the first page load, but not able to maintain the data on post back, i want that on the next click i want another question display on my page.
public partial class MCQ : System.Web.UI.Page
{
static private int i = 0;
private SqlConnection MyConnection;
private SqlCommand MyCommand;
private SqlDataAdapter MyAdapter;
private DataTable MyTable = new DataTable();
private DataSet MyDataSet = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
// Count max colums in questionaries table
// Create a array of same size = MAX_TEST_SIZE
// Fill array with random number MAX_ELEMENTS shuold be
//Random rd = new Random(1);
//for(int i =1; i<10; i++)
//System.Console.WriteLine(rd.Next());
if (i == 0)
Previous.Enabled = false;
else
Previous.Enabled = true;
if (!Page.IsPostBack)
{
// Connection Properties
MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["healingDBConnectionString"].ConnectionString;
MyCommand = new SqlCommand();
MyCommand.CommandText = "Select * from Questionaries";
MyCommand.CommandType= CommandType.Text;
MyCommand.Connection = MyConnection;
MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = MyCommand;
MyAdapter.Fill(MyDataSet,"Questionaries");
DisplayQuestion(i++);
gvCustomers.DataSource = MyTable.DefaultView;
gvCustomers.DataBind();
//MyAdapter.Dispose();
//MyCommand.Dispose();
//MyConnection.Dispose();
}
}
public void DisplayQuestion(int i)
{
DataRow pRow = MyDataSet.Tables["Questionaries"].Rows[i];
Question.Text = pRow["Question"].ToString();
QuestionNO.Text = pRow["SerialNo"].ToString();
Option1.Text = pRow["Option1"].ToString();
Option2.Text = pRow["Option2"].ToString();
Option3.Text = pRow["Option3"].ToString();
Option4.Text = pRow["Option4"].ToString();
}
protected void Previous_Click(object sender, EventArgs e)
{
DisplayQuestion(i);
i--;
}
protected void Next_Click(object sender, EventArgs e)
{
DisplayQuestion(i);
i++;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MCQ.aspx.cs" Inherits="MCQ" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Welcome" runat="server" Text="Welcome"></asp:Label>
<p>
<asp:Label ID="QuestionNO" runat="server" Text="QuestionNO "></asp:Label>
<asp:Label ID="Question" runat="server" Text="Question"></asp:Label>
</p>
<p>
<asp:RadioButton ID="Option1" GroupName="Questionaries" runat="server" />
</p>
<p>
<asp:RadioButton ID="Option2" GroupName="Questionaries" runat="server" />
</p>
<asp:RadioButton ID="Option3" GroupName="Questionaries" runat="server" />
<p>
<asp:RadioButton ID="Option4" GroupName="Questionaries" runat="server" />
</p>
<p>
<asp:Button ID="Previous" runat="server" Text="Previous" Width="100"
onclick="Previous_Click" />
<asp:Button ID="Next" runat="server" Text="Next" Width="100"
onclick="Next_Click" />
<asp:GridView ID="gvCustomers" runat="server" />
</p>
</form>
</body>
</html>
I wan that when i click on next , some other question should be display.