Help with consultation in AJAX

1

I am using ASP.NET, C # and AJAX. What I need to do is send a number of n questions for an exam, where that amount n is a number that the administrator previously registered in a form that sends the data to a database. I already managed to get him to send the questions randomly from the selected topic, the problem is that I do not know how to send the number of questions that the administrator indicated.

That number of questions that will contain the exam are stored in a SQL Server 2012 table called dbo.tema . In the following image you will see that there are 2 subjects (exams) entered for the test, with a number of 10 questions for each one. Therefore, when displaying the exam form, you should show only 10 random questions.

I have a method to list the questions of the topic made in C # with parameters that I send to the database:

public List<Pregunta> ListarPreguntas(String codTema, String noPreguntas)
        {
            List<Pregunta> Lista = new List<Pregunta>();
            SqlConnection con = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;


            try
            {
                con = Conexion.getInstance().ConexionBD();
                con.Open();
                cmd = new SqlCommand("spListarPreguntasPorTema", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@prmLimite", noPreguntas);
                cmd.Parameters.Add("@prmCodTema", codTema);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    //Creando objetos de Tema
                    Pregunta objTema = new Pregunta();
                    objTema.ID = Convert.ToInt32(dr["cod_pregunta"].ToString());
                    objTema.Preg = dr["pregunta"].ToString();

                    objTema.respuestas = RespuestaDAO.getInstance().ListarRespuestasPorPregunta(objTema.ID);


                    //Añadiendo a la lista de objetos
                    Lista.Add(objTema);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return Lista;
        }

In the stored procedure I have the following:

ALTER PROCEDURE [dbo].[spListarPreguntasPorTema]
(@prmLimite int,
@prmCodTema int
)
AS
    BEGIN
        SELECT TOP (@prmLimite) p.cod_pregunta, p.pregunta
        FROM dbo.pregunta AS p
        INNER JOIN dbo.tema AS t
        ON p.cod_tema = @prmCodTema AND @prmLimite = t.no_preguntas
        ORDER BY NEWID()
    END

Where I send to call the questions and they are sorted randomly.

And now in my .js I have the following method:

function cargarExamen(codTema) {
    var obj = JSON.stringify({
        codTema: codTema,
        noPreguntas: 9
    });

    $.ajax({
        type: "POST",
        url: "Examen.aspx/ListarPreguntas",
        data: obj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr, ajaxOptions, thrownError) {
            (xhr.status + " \n" + xhr.responseText, "\n" + thrownError);
        },
        success: function (data) {
            var c = data.d;
            var htmlC = "";
            for (var n = 0; n < c.length; n++) {
                htmlC += '<div class="col-md-12"><b>' + c[n].Preg + '</b><br>';

                for (var n2 = 0; n2 < c[n].respuestas.length; n2++) {
                    var respuesta = c[n].respuestas[n2];

                    htmlC += '<div class="col-md-3"><input id="optPreg' + c[n].ID + respuesta.ID + '" type="radio" name="optPreg' + c[n].ID + '" value="' + respuesta.ID + '">'+
                    '&nbsp;&nbsp;&nbsp;<label for="optPreg' + c[n].ID + respuesta.ID + '">' + respuesta.Resp + '</label>';
                    htmlC += '</div>'

                }
                htmlC += '</div>';
            }

            $('.lstTema').css("display", "none");
            $('.lstExamen').css("display", "inline");
            $('#cntExamen').html(htmlC);
        }
    });
}

In the part of noPreguntas: 9 it is like this because it was a static data that I stopped testing, what I need is that instead of a static number it is the number of questions that the administrator indicated.

    
asked by Oscar Anibal 01.10.2018 в 19:24
source

1 answer

1

Simplify the problem. Since who knows the amount of questions is the subject, do not send an order with the number of questions.

Your method will list questions, you will only receive the topic to list.

public List<Pregunta> ListarPreguntas(String codTema)

And the first thing you're going to do is find out how many questions you need:

cmd = new SqlCommand("select no_preguntas from tema where cod_tema = @cod_tema", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@cod_tema", codTema);
int cant_preguntas = (int)cmd.ExecuteScalar();

Once you do that, in your variable cant_preguntas , you already have the amount you need, so you do not need to send it from your front-end

    
answered by 01.10.2018 / 19:32
source