Reputation: 51
I want to upload a pdf file from a scanner into a database (SQL Server), but I get the error message:
System.ArgumentException: 'No mapping exists from object type System.__ComObject to a known managed provider native type.'
I'm using Visual Studio 2019.
This is my code:
escanerDisponible = manejadorDisp.DeviceInfos[i];
Device dispositivo = escanerDisponible.Connect();
Item objetoEscaneado = dispositivo.Items[1];
Escaner.AjustesEscaner(objetoEscaneado, resolucion, 0, 0, width, height, 0, 0, color);
con.Open();
SqlCommand inserta = new SqlCommand("UPDATE inspeccionGestionDocumental SET numeroInspeccion = numeroInspeccion, numeroPropuesta = numeroPropuesta, placa = placa, documento = @documento WHERE placa ='" + tNumeroPlaca.Text + "'AND numeroPropuesta ='" + tNumeroPropuesta.Text + "';", con);
inserta.Parameters.AddWithValue("@documento", objetoEscaneado);
SqlParameter addDocumento = new SqlParameter("@documento", objetoEscaneado);
addDocumento.Direction = ParameterDirection.Output;
inserta.ExecuteReader();
MemoryStream ms = new MemoryStream();
inserta.ExecuteReader().Read();
var bytes = new Byte[(inserta.ExecuteReader().GetBytes(0, 0, null, 0, int.MaxValue))];
inserta.ExecuteReader().GetBytes(0, 0, bytes, 0, bytes.Length);
ms.Write(bytes, 0, bytes.Length);
MessageBox.Show("Documento insertado");
con.Close();
Upvotes: 1
Views: 3212
Reputation: 172280
Here:
inserta.Parameters.AddWithValue("@documento", objetoEscaneado);
You add an object of type Item
as a parameter to your SQL. ADO.NET does not know how to convert Item
into a data type that SQL Server understands ("a known managed provider native type"), which is why you get the error message.
To fix this, use a data type that SQL Server understands (for example, a string or a byte array) instead. I am not familiar with the library you use, but it's very likely that Item
has a property or method that allows you to access the document as a byte array.
Oh, and as a side note: Please use parameters for your WHERE parameters as well, see this question for details:
Upvotes: 2