我正在使用SQL服务器,我有以下2个数据库表:
现在我想要从DB显示问题以及答案。 但下一个要显示的问题将取决于之前的问题的答案。
假设Q1用户选择YES - > Q2将出现在下一个。
但是如果用户选择NO - > Q3会出现。
以下是我的代码:
public partial class Recommendation : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
DataTable dt = new DataTable();
int rowIndex = 0;
int QuesIndex = 0;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = (ConfigurationManager.ConnectionStrings["RecommendationConnectionString"].ConnectionString);
con.Open();
PopulateDataTable();
if (!IsPostBack)
{
if (dt.Rows.Count > 0)
{
Label2.Text = dt.Rows[0]["Question"].ToString();
HiddenField2.Value = dt.Rows[QuesIndex]["Question_ID"].ToString();
HiddenField1.Value ="1";
}
BindList();
//QuesIndex++;
}
if (Request.Form["HiddenField1"] != null)
rowIndex = Convert.ToInt32(Request.Form["HiddenField1"].ToString());
if (rowIndex == dt.Rows.Count && rowIndex != 0)
{
rowIndex = 0;
HiddenField1.Value = Convert.ToInt32(0).ToString();
}
con.Close();
}
private void BindList()
{
DataSet DS1 = new DataSet();
string command = "Select ID, Answers from Question_Answer where Ques_ID = '" + HiddenField2.Value + "'";
//command.Parameters.AddWithValue("@Ques_ID", Ques_ID);
SqlDataAdapter dataAdaptor = new SqlDataAdapter(command, con);
dataAdaptor.Fill(DS1);
RadioButtonList1.DataSource = DS1;
RadioButtonList1.DataTextField = "Answers";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
}
private void PopulateDataTable()
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("Select Question_ID, Question from Questionnaire", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sDA = new SqlDataAdapter();
sDA.SelectCommand = cmd;
sDA.Fill(ds, "Questionnaire");
dt = ds.Tables[0];
}
protected void Button1_Click(object sender, EventArgs e)
{
if (dt.Rows.Count > 0)
{
Label2.Text = dt.Rows[rowIndex]["Question"].ToString();
HiddenField2.Value = dt.Rows[QuesIndex ["Question_ID"].ToString();
rowIndex++;
QuesIndex++;
HiddenField1.Value = rowIndex.ToString();
}
Session["QnA"] = HiddenField2.Value;
}
此代码只是从Db调查问卷中获取问题1。但我希望根据用户之前的答案显示下一个问题。
我该怎么做?