While working with an Ajax request in JavaScript, I encountered an issue where the data being fetched is repeating in the last three fields. Upon validating the query in the database, it seems that the data retrieved by the Ajax call does not match what is stored. Here are the functions I am using:
In my asp.net application, I have the following function:
[HttpGet]
public List<Reportes> GetScrapReport(string fecha, string fechaend)
{
try {
var fechaparametro = new SqlParameter("@fecha", fecha);
var fechafinparametro = new SqlParameter("@fechafin", fechaend);
var listareport = _context.Reportes.FromSqlRaw($"SELECT DISTINCT idscrap, fecha, modelo, elemento, nombre, numeroparte, cantidad FROM F_GetScrapReport (@fecha, @fechafin)", fechaparametro, fechafinparametro);
return listareport.ToList();
}
catch
{
return new List<Reportes>();
}
}
The structure of the Reportes
model is as follows:
public class Reportes
{
[Key]
public int Idscrap { get; set; }
public DateTime fecha { get; set; }
public string modelo { get; set; }
public string elemento { get; set; }
public string? nombre { get; set; }
public string? numeroparte { get; set; }
public int? cantidad { get; set; }
}
Here's the AJAX JavaScript function I've implemented:
function GetScraptime()
{
// Function code here
}
The SQL function used to retrieve data from the database is defined as follows:
CREATE FUNCTION F_GetScrapReport (@fecha varchar(20), @fechafin varchar(20))
// SQL Function Code here
When executing the SQL query, the results obtained differ from what is expected and lead to repeated values in certain columns. This inconsistency between the DB data and the displayed results can be seen below:
https://i.sstatic.net/wvKPD.png
This discrepancy is evident in the output where certain columns like nombre, numeroparte, and cantidad exhibit duplicate values which do not align with the actual database records.