Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hello everyone,

I have one database in which there is a table called question and another table called question_answers

there are 2 types of questions namely : multiple choice and single response..

I want to fetch the question and its answers using JSON ..
I am retrieving data using WebMethod from SQL database

my code is : JSON
function take_test(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "LoadTest.aspx/GetQuiz",
        data: "{}",
        dataType: "json",
        success: function (data) {
            //            for (var i = 0; i < data.d.length; i++) {
            //                $("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>");
            //            }
            alert(data);
        },
        error: function (result) {
            alert(result);
        }
    });
}


code behind :

  [WebMethod]
    // Let .net handle the request and response as json automatically.
    public static FetchQuiz[] GetQuiz(FetchQuiz q)
    {
        DataSet ds = new DataSet();
        List<FetchQuiz> details = new List<FetchQuiz>();
        SQLDataAccessHelper sq = new SQLDataAccessHelper();
        string query = "";
        query = "select * from tbl_question_master order by Id";
        ds = sq.ExecuteCommandText(query);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            FetchQuiz quiz = new FetchQuiz();
            quiz.Question = ds.Tables [0].Rows[i]["question"].ToString();
            quiz.Answer = ds.Tables[0].Rows[i]["answer"].ToString();
           // quiz.CorrectAnswer = ds.Tables[0].Rows[i]["UserId"].ToString();
            details.Add(quiz);
        }

        return details.ToArray();
    }
}
public class FetchQuiz
{
    public string Question { get; set; }
    public string Answer { get; set; }
   // public string CorrectAnswer { get; set; }
    //public string Type { get; set; }

}


Now the problem here is i m not able to get data... also i am not aware how to iterate with data..

my data would be in this format

Question Id Type Question Answer
1 MCQ abc a
1 MCQ abc b
1 MCQ abc c
2 SR xyz a
2 SR xyz b


i want to display data in div tag in this format
[Question]
A [Option1]
B [Option2]
C [Option3]

and so on...

Please help me to achieve this.

Thanks in advance

Krunal
Posted
Comments
JoCodes 7-Feb-14 9:43am    
The code looks ok. Is there any error while iterating through the Objects?
krunalpanchalN 7-Feb-14 9:47am    
Hi,

It doesnt works.. it shows me blank message box.. though i have data in my database

Please help..

JoCodes 7-Feb-14 9:51am    
Tell me whether you have data in this function (data) ajax call?
krunalpanchalN 7-Feb-14 9:52am    
Yes i do have data...
JoCodes 7-Feb-14 9:56am    
That means the WebMethod returns collection correctly . What about the Commented Code in Ajax call for iteration?

1 solution

Look at the link below. Basically it's a javascript plugin called knockout.
it binds data to html controls, so in your problem you can e.g. do
HTML
<div databind="foreach: { data: question, as 'question' }"></div>

then some html to render each question e.g. as
HTML
<ul><li>Question 1: blah blah</li></ul>


If you have a little time to lear it, it's the best thing to handle json data to html.

Knockout from json[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900