locked
object is not returning from web api to controller RRS feed

  • Question

  • User-1722502126 posted

    Hi 

    My web api  Method is not returning object to controller

    This is Code snipet

    public IHttpActionResult GetMockTestDataByCrsCdExamCd([FromUri]string scrtcd, [FromUri]int Qnumber, [FromUri]string Exmcd, [FromUri]string Pssmrk)
    {
    try
    {
    using (Models.EOTAEntities dbModel = new Models.EOTAEntities())
    {
    if (Pssmrk.ToLower() == "nok")
    {
    if (Qnumber > 0)
    {
    var qry = from row in dbModel.Mock_Tests orderby row.Ques_No select row;

    int count = qry.Count(); // 1st round-trip
    int index = new Random().Next(count);

    var cust = qry.Skip(index).FirstOrDefault();
    Console.WriteLine(cust);
    return Content(HttpStatusCode.OK, cust);

    }
    else
    {
    var qtp = (from x in dbModel.Mock_Tests
    where x.Course_Code == scrtcd && x.Exam_Code == Exmcd && x.EnableDisable == true && x.Question_Type == "T"
    select x.Question_Type).FirstOrDefault();
    if (qtp != null)
    {
    return Content(HttpStatusCode.OK, "Question type is text");
    }
    else
    {
    return Content(HttpStatusCode.NoContent, "Question type is not text");
    }
    }
    }
    else
    {
    var pssmrk = (from x in dbModel.Mock_Tests
    where x.Course_Code == scrtcd && x.Exam_Code == Exmcd && x.EnableDisable == true
    select x.Pass_mark).FirstOrDefault();
    return Content(HttpStatusCode.OK, pssmrk);
    }
    }

    }
    catch(Exception E)
    {
    return BadRequest();
    }

    }

    return Content(HttpStatusCode.OK, cust); after executing this line. it is getting terminated, and it's showing internal server error

    i m unable to resolve it. please any one help me. thanks in advance.

    Thursday, June 20, 2019 9:46 AM

All replies

  • User-1038772411 posted

    Hi, DARSHAN KUMARAB

    var qry = from row in dbModel.Mock_Tests orderby row.Ques_No select row;
    
    int count = qry.Count(); // 1st round-trip
    int index = new Random().Next(count);
    
    var cust = qry.Skip(index).FirstOrDefault();
    Console.WriteLine(cust);
    return Content(HttpStatusCode.OK, cust);

    For my understanding if your "qry" paramater return null than count 0 and also ahead process returing error. so please debug it and see what actual error. may be you have to handel null value for error handling.

    Thanks 

    Thursday, June 20, 2019 10:20 AM
  • User1120430333 posted

    In the future, you should format the code you are posting so that it's readable easily. You can do this before making the post by using the {;} icon on the toolbar to format the code that you want in the post.

    Thursday, June 20, 2019 10:55 AM
  • User-1722502126 posted

    my "qry" parameter not return null than count 61

    and my "index" is 40

    and "cust" parameter is 40 row data

    return Content(HttpStatusCode.OK, cust);

    After this line execution it's terminated
    Thursday, June 20, 2019 11:02 AM
  • User-1722502126 posted

    Thanks For suggestion.....

    Thursday, June 20, 2019 11:03 AM
  • User-1038772411 posted

    Based on your question :

    int count = qry.Count(); // 1st round-trip
    int index = new Random().Next(count);
    
    //var cust = qry.Skip(index).FirstOrDefault();
    
    var cust = qry.Skip(index).ToList();
    
    //var cust = qry.Skip(index).ToListAsync();
    
    return Content(HttpStatusCode.OK, cust);

    may be you got null record or value for first row or may be it's not found so you  have to try "ToList()". may be this will help you.

    Thanks. 

    Thursday, June 20, 2019 11:21 AM
  • User-1722502126 posted

    i Need to fetch ro in random it's fetch only one row 

    in case we use Tolist it's skip index number of data and fetch remain data ToList

    Example:

    count = 61;
    Index = 24;

    "cust" parameter only append remain 37 Rows,

    it means 61-24=37

    Thursday, June 20, 2019 12:34 PM
  • User753101303 posted

    Hi,

    If you have a "bad request" response this is what you asked for when an exception happens (which is likely a bad idea). If an exception happens, the first step would be to be able to see what it is (by default it should go to the Windows event log). Unless seeing something obvious it much easier to fix a problem knowing first what it is (which usually takes a couple of minutes).

    You could likely use https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019 and see which exception you have in E. BTW I would expect return Ok(cust);

    Thursday, June 20, 2019 1:00 PM