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();
        }
DOK
  • 32,337
  • 7
  • 60
  • 92
Shewn
  • 1
  • 1
  • 1
  • 2
    "but its not working well" is a very poor description of the problem. What is the actual issue? Please read [this article](https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx) before updating your question. – Oded May 02 '12 at 11:54
  • Does LabelRadio1 is radiobuttonlist? – Fraz Sundal May 02 '12 at 12:07
  • d LabeRadio1 is a custom control i created – Shewn May 03 '12 at 14:18

1 Answers1

1

try this code this may help you

protected void Page_Load(object sender, EventArgs e)

{ DataTable dt = new DataTable();

          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;
    } 
Rejeesh
  • 1,236
  • 12
  • 15
  • tried it, got the error: Cannot implicitly convert type 'string' to 'System.Collections.Generic.List' at this point: 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(); } – Shewn May 03 '12 at 14:27