locked
Error-There is already an open DataReader associated with this Command which must be closed first. RRS feed

  • Question

  • User-1499457942 posted

    Hi

     Below is the code

    function FillCity() {
            var stateId = $('#StateId').val();
            $.ajax({
                url: '/Customer/GetCity',
                type: "GET",
                dataType: "JSON",
                data: { StateId: stateId },
                success: function (cities) {
                    $("#CityId").html(""); // clear before appending new list 
                    $.each(cities, function (i, city) {
                        $("#CityId").append(
                            $('<option></option>').val(city.CityId).html(city.CityName));
                    });
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    $("#errorModalBody").html('Status : ' + xhr.status + ' Error : ' + thrownError);
                    $("#errorModal").modal('show');
                }
            });
        }
    
    Controller
    public ActionResult GetCity(int StateId)
            {
                var cities = db.Cities.Where(c => c.StateId == StateId);
                return Json(cities, JsonRequestBehavior.AllowGet);
            }

    Thanks

    Friday, April 14, 2017 3:06 PM

All replies

  • User765422875 posted

    This error can show up if you execute a query while iterating over the results from another query, but from your example its not clear if this is the actual issue.

    Allowing MARS in your connection string may solve the issue. Add 

    MultipleActiveResultSets=true

     to the provider part of your connection string.

    Friday, April 14, 2017 3:55 PM
  • User-1499457942 posted

    Hi

      I am getting server error 500

    Friday, April 14, 2017 4:12 PM
  • User765422875 posted

    Thats an internal server error. Where is the error happening? What line of code specifically?

    Friday, April 14, 2017 4:17 PM
  • User-707554951 posted

    Hi JagjitSingh,

    Two commonly used methods for a request-response between a client and server are: GET and POST.

    • GET - Requests data from a specified resource
    • POST - Submits data to be processed to a specified resource

    GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.

    POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

    https://www.w3schools.com/jquery/jquery_ajax_get_post.asp

    For your 500 error:  you  could change your request type to Post and ensure that the Controller Action is decorated with the [HttpPost]

    attribute, indicating it should be used for POST calls :

    Besides, you could  read the  correct reply in the following link to check your code:

    http://stackoverflow.com/questions/36894273/jquery-ajax-call-to-pass-json-array-in-mvc-500-internal-server-error

    Best regards

    Cathy

    Monday, April 17, 2017 2:11 AM