0

I am developing an online examination system, but im having difficulties trying to read questions from the database to be displayed on an aspx on page load. Please help me out...wat do i do...i tried this code but its not working well.

    string cs = ConfigurationManager.ConnectionStrings["OnlineExamDBCS"].ToString();
        SqlConnection conn = new SqlConnection(cs);
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;


  cmd.CommandText = "SELECT questionTitle, Answer1, Answer2, Answer3, Answer4, Answer5 
        FROM tblQuestions WHERE CourseCode = \'" + question + "\'";
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            LabelRadio1.Questions = reader["questionTitle"].ToString();
            LabelRadio1.Answers = reader["Answer1"].ToString();
            LabelRadio1.Answers = reader["Answer2"].ToString();
            LabelRadio1.Answers = reader["Answer3"].ToString();
            LabelRadio1.Answers = reader["Answer4"].ToString();
            LabelRadio1.Answers = reader["Answer5"].ToString();
        }
4

1 回答 1

1

试试这个代码,这可能对你有帮助

protected void Page_Load(object sender, EventArgs e)

{ 数据表 dt = 新数据表();

          string select_qry = "SELECT questionTitle, Answer1, Answer2, Answer3, Answer4, Answer5  FROM tblQuestions WHERE CourseCode = \'" + question + "\'";
          SqlCommand cmd = new SqlCommand(select_qry);
          dt= GetData(cmd);
          if (dt.Rows.Count > 0)
          {
              LabelRadio1.Questions = dt.Rows[0]["questionTitle"].ToString();
              LabelRadio1.Answers = dt.Rows[0]["Answer1"].ToString();
              LabelRadio1.Answers = dt.Rows[0]["Answer2"].ToString();
              LabelRadio1.Answers = dt.Rows[0]["Answer3"].ToString();
              LabelRadio1.Answers = dt.Rows[0]["Answer4"].ToString();
              LabelRadio1.Answers = dt.Rows[0]["Answer5"].ToString(); 
          }


    }
    public DataTable GetData(SqlCommand cmd)
    {
        string sqlCon =System.Configuration.ConfigurationManager.ConnectionStrings["OnlineExamDBCS"].ToString();
        SqlConnection Con = new SqlConnection(sqlCon);
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = Con;
        Con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    } 
于 2012-05-02T12:05:49.633 回答