I'm passing an array of JSON data from client-side to server-side (c#). However when I try to insert this data into a SQL table I'm having this Newtonsoft.Json.Linq.JValue just at the moment of the query execution.
Here's the code I have so far:
Client (js);
var json = [];
var Firstcotizacion = {
idCotizacion: "111111",
idProyecto: "8047",
nombreProyecto: "Edificio Caiquen",
idProducto: "MLC462815278"
}
var Secondcotizacion = {
idCotizacion: "222222",
idProyecto: "1234",
nombreProyecto: "Edificio malbec",
idProducto: "MLC29870FD"
}
json.push(Firstcotizacion)
json.push(Secondcotizacion)
$.post("../Ajax/GuardaCotizacionesPI", { json: JSON.stringify(json) }, function (data) {
console.log(data)
});
Server-side (C#):
public string GuardarCotizacion(string json)
{
string SP_INSERTA_COTIZACIONPI = "SVI_CPI_COTIZACION_PI";
dynamic cotizaciones = JsonConvert.DeserializeObject(json);
foreach (var cotizacion in cotizaciones)
{
using (SqlConnection conexion = new SqlConnection(Inventa.PazCorp.Data.BaseDatos.StringConexionGestionContactos(System.Configuration.ConfigurationManager.AppSettings["ambiente"].ToString())))
{
conexion.Open();
using (SqlCommand comm = new SqlCommand(SP_INSERTA_COTIZACIONPI, conexion))
{
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.AddWithValue("IDCOTIZACION", cotizacion.idCotizacion);
comm.Parameters.AddWithValue("IDPROYECTO", cotizacion.idProyecto);
comm.Parameters.AddWithValue("NOMBRE_PROYECTO", cotizacion.nombreProyecto);
comm.Parameters.AddWithValue("IDPRODUCTO", cotizacion.idProducto);
comm.ExecuteNonQuery(); //Here is where the error appears
}
}
}
return json;
}
Any Idea what could be wrong?
/Ajax/GuardaCotizacionesPIbut then you show a method calledGuardarCotizacion. How is the 1st connected to the 2nd?dynamicthough? Use a concrete class and all this goes away. Also, every time you usedynamic, a kitten dies.