1

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();
GSerg
  • 76,472
  • 17
  • 159
  • 346
luciacar
  • 51
  • 8
  • 1
    please use ***parameterised queries*** - building SQL queries by concatenation etc. is a recipe for disaster. not only is it a source for many hard to debug syntax errors, it's also a wide, open gate for ***[SQL Injection attacks](https://bobby-tables.com/)***. – Franz Gleichmann May 31 '21 at 15:37

1 Answers1

2

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:

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I added the Item object as a string to the SQL Command and now I don't get that message error, thank you! – luciacar May 31 '21 at 16:03
  • 1
    @luciacar: How did you do that? I'm not sure that this does what you expect it to do. – Heinzi May 31 '21 at 16:17
  • I've just noticed that the pdf that I get from the scanner is not been inserted correctly into the database, I just I get ride of the error – luciacar May 31 '21 at 16:49
  • 1
    @luciacar: That's what I suspected. You need to get a byte array of your pdf. To do that, you need to check the methods/properties of your `objetoEscaneado` (or the documentation of your scanner API). – Heinzi May 31 '21 at 17:01
  • objetoEscaneado is the pdf that I scan, and I save it as an Item. – luciacar Jun 01 '21 at 14:18
  • 1
    @luciacar: Yes, I understand that. What exactly is an `Item`? What are its properties and methods? – Heinzi Jun 01 '21 at 14:28
  • it's a type that comes when I added using `WIA` – luciacar Jun 01 '21 at 14:57
  • 1
    @luciacar: I see. In that case, the simplest solution might be to use [Item.Transfer](https://learn.microsoft.com/en-us/windows/win32/wia/-wia-iwiadispatchitem-transfer) to save the image to a temporary file on disk, read it with `File.ReadAllBytes` and then pass the byte array to your command. – Heinzi Jun 01 '21 at 15:58
  • something like this? `ImageFile file = (ImageFile)objetoEscaneado.Transfer(path); var read = File.ReadAllBytes(path); SqlCommand inserta = new SqlCommand("UPDATE inspeccionGestionDocumental SET numeroPropuesta = numeroPropuesta, placa = placa, numeroInspeccion = numeroInspeccion, documento = @documento;", con); inserta.Parameters.AddWithValue("@documento", read); inserta.ExecuteNonQuery();` – luciacar Jun 01 '21 at 16:26