Mufid Bagdadi

Mufid Bagdadi

  • NA
  • 146
  • 24.3k

Reading data from SQL DATABASE as per condition.

Jul 5 2016 5:09 AM
Actually i want to call each question from database table one by one on button click, i have written a code which display 1st question when i click on button it shows 2nd question but when i click the next button it doesn't  response please review my code,
 
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Drawing;

public partial class endUser_quizPage : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["quizDatabase"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;

string aa;
string ab;
string tn;
int x = 1;
int a = 0;

protected void Page_Load(object sender, EventArgs e)
{
question();
}

private void question()
{
srNo.Text = x.ToString();
try
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "select question, option1, option2, option3, option4 from quiz where srNo = '"+ x +"'";
dr = cmd.ExecuteReader();
dr.Read();
ques.Text = dr[0].ToString();
option01.Text = dr[1].ToString();
option02.Text = dr[2].ToString();
option03.Text = dr[3].ToString();
option04.Text = dr[4].ToString();
dr.Close();
con.Close();
report.Visible = false;
}
catch (Exception er)
{
report.Text = er.Message;
report.Visible = true;
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (x <= 15)
{
if (a == 1)
{
ClientScript.RegisterStartupScript(GetType(), "Review your question", "alert(\'You have to attempt the question to proceed.');", true);
report.Text = "Please attempt the question.";
report.Visible = true;
}
else
{
x = x + 1;
question();
}
}
else
{
report.Text = "15 Completed.";
report.Visible = true;
}
}
}
 
Waiting for your kind reply.

Answers (9)