none
Intermittent data and what to look for in WebApi RRS feed

  • Question

  • When testing my application I'm seeing different data set every time even for the same region(Intermittent). There is a different count in the data set. What should I look for in the API for a situation like this. Would it authentication, not sure because it happens to all users and not just one.

    In reproducing the issue in the app by selecting a customer and then select a vehicle/unit no. for that customer. When selecting vehicle/unit on one of the customer it shows 4 unit numbers which is correct. I go out of the unit selection screen without selecting a unit and then select the same customer again and it may show 3 or 4. Most times I test it shows 4 but sometimes it will show 3. For example one time I did this like 20 times before it showed 3 units. That's how I'm reproducing it. It doesn't happen every time but it does happen intermittently.

    I think the problem may be in here.

    public dynamic GetFleetUnitNumberAndVINByCustomer(RegionParam regionParam)
        {
            try
            {
                var fleetDetails = new List<CustomerFleetUnitVIN>();
    
                SqlConnection conn = new SqlConnection(ApplicationDbContext.ConnectionString);
                SqlDataAdapter da = new SqlDataAdapter();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "GetFleetUnitNumberAndVINByCustomer";
                da.SelectCommand = cmd;
    
                conn.Open();
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, cmd.CommandText, new SqlParameter("@REGION", regionParam.Region.ToString()));
                conn.Close();
    
                DataTable dt = ds.Tables[0];
                Parallel.ForEach(dt.Rows.Cast<DataRow>(), dr =>
                {
                    var fleetInfo = new CustomerFleetUnitVIN
                    {
                        Unit = (dr["Unit"] == DBNull.Value) ? string.Empty : Convert.ToString(dr["Unit"]),
                        VIN = (dr["VIN"] == DBNull.Value) ? string.Empty : Convert.ToString(dr["VIN"]),
                        CustomerNumber = (dr["CustomerNumber"] == DBNull.Value) ? string.Empty : Convert.ToString(dr["CustomerNumber"]).Trim()
                    };
                    fleetDetails.Add(fleetInfo);
                });
                return fleetDetails;
            }
            catch (Exception e)
            {
                throw;
            }
    
        }

    Here is part of the result set that I run in Postman, keep in mind the result is normally over 260000 lines:

    Sometimes when I hit send in postman I will get less lines and when that happens and I scroll up to see some of the results i see nulls in the results. I think this is where it may show less units on the app. Does that make sense and can you see any problems based off my code. Here is part of the result set:

      {
            "unit": "N41762",
            "vin": "2FZHAZCV14AN41762",
            "customerNumber": "700150"
        },
        {
            "unit": "N48",
            "vin": "10102",
            "customerNumber": "700015"
        },
        {
            "unit": "N50307",
            "vin": "13N1452C641520258",
            "customerNumber": "700458"
        },
        {
            "unit": "N50347",
            "vin": "13N1452C851527617",
            "customerNumber": "700461"
        },
        {
            "unit": "N50348",
            "vin": "13N1452CX51527618",
            "customerNumber": "700458"
        },

    Any ideas on how to fix this issue would be appreciate
    • Edited by bootzilla Thursday, April 18, 2019 6:16 PM
    Thursday, April 18, 2019 6:12 PM

All replies

  • When testing my application I'm seeing different data set every time even for the same region(Intermittent). There is a different count in the data set. What should I look for in the API for a situation like this. Would it authentication, not sure because it happens to all users and not just one.

    Why are you using a dataset and datatable in a WebAPI solution?

    https://dzone.com/articles/reasons-move-datatables

    You can use a datareader using columnname.

    http://www.java2s.com/Code/CSharp/Database-ADO.net/ReferencedatainSqlDataReaderbycolumnname.htm

    Why are you using Dynamic as the return type  of the method and why are you not using a concrete type like a DTO that is known by the WebAPI client and the WebAPI?

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    IMHO, you could be using the ADO.NET Entity Framework and not datasets and datatable, which can execute a stored procedure,  and most WebAPI tutorials are centered around using EF.

    The DTO is kept in a classlib project called Entities and the WebAPI client and WebAPI have reference to Entities.

    Of course as you know, WebAPI can be discussed at the ASP.NET forums, since I have seen you positing there bootzilla.

    Example....

    using System;
    
    namespace Entities
    {
        public class DtoProject
        {
            public int ProjectId { get; set; }
            public string ClientName { get; set; }
            public string ProjectName { get; set; }
            public string Technology { get; set; }
            public string ProjectType { get; set; }
            public string UserId { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public decimal Cost { get; set; }
        }
    }

     public List<DtoProject> GetProjectsByUserId(string userid)
            {
                var dtos = new List<DtoProject>();
    
                using (var context = new ProjectManagementContext(_options))
                {
                    
                    dtos = (from a in context.Projects.Where(a => a.UserId.Contains(userid))
                        select new DtoProject
                        {
                            ProjectId = a.ProjectId,
                            ClientName = a.ClientName,
                            ProjectName = a.ProjectName,
                            Technology = a.Technology,
                            ProjectType = a.ProjectType,
                            UserId = a.UserId,
                            StartDate = a.StartDate,
                            EndDate = a.EndDate,
                            Cost = a.Cost
                        }).ToList();
                }
    
                return dtos;
            }

    [HttpGet]
            [Route("GetProjsByUserId")]
            public List<DtoProject> GetProjectsByUserId(string userid)
            {
                return _daoProject.GetProjectsByUserId(userid);
            }

    public List<DtoProject> GetProjsByUserIdApi(string userid)
            {
                var dtoprojects = new List<DtoProject>();
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://progmgmntcore2api.com/api/project/GetProjsByUserId?userid=" + userid);
    
                    var response = client.GetAsync(uri).Result;
    
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
    
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
    
                    dynamic projects = JArray.Parse(responseString) as JArray;
    
                    foreach (var obj in projects)
                    {
                        DtoProject dto = obj.ToObject<DtoProject>();
    
                        dtoprojects.Add(dto);
                    }
                }
    
                return dtoprojects;
            }

    Thursday, April 18, 2019 7:45 PM