1

I am trying to insert a image to my online database but on insertion the database closes my connection during the execution. I have tested other insert statements with other tables on the same database and works fine. I have tried changing the database type to varbinary(MAX) and image. Still the same error. My image is getting saved in a byte [] variable and then inserted with AddWithValue.

Here is my code to convert a signature to a byte []:

var signatureImage = sig.GetImage();

        byte[] bitmapData;
        using (var memStream = new MemoryStream())
        {
            signatureImage.Compress(Bitmap.CompressFormat.Jpeg, 50, memStream);
            bitmapData = memStream.ToArray();
        }

Here is my code for adding into a parameter for my SQL command:

command.Parameters.AddWithValue("@waiverSigned", user.waiverSigned);

The Error I am getting: System.Data.SqlClient.SqlException (0x80131904): Server closed the connection.

Environment: Xamarin Android

Let me know if you need anything else.

  • Have you tried to specify parameter type as varbinary? - [see example here](http://stackoverflow.com/a/1088630/5331361) – Denis Krasakov Mar 26 '17 at 21:45
  • @DenisKrasakov I just tried that and still giving me the same error. I am wondering if its to much data on one thread (UI Thread). We tried inserting just a byte array and a number to newly created table and it worked. So I wonder if its too much on one thread. – gojacks2323 Mar 26 '17 at 22:07
  • Maybe something here would help you: http://stackoverflow.com/questions/5824620/what-sqldbtype-maps-to-varbinarymax – ZLK Mar 26 '17 at 23:46
  • @ZLK This doesn't solve my issue. I open the connection, I use `ExecuteNonQuery()` but during that query an error is thrown saying `System.Data.SqlClient.SqlException (0x80131904): Server closed the connection.` – gojacks2323 Mar 27 '17 at 00:43
  • 2
    Generally it is bad practice to use database directly from mobile client. I suggest to make small REST service that handle image uploading and other stuff. But if you want to use database command directly try to increase timeouts for connection and command – Denis Krasakov Mar 27 '17 at 08:29

0 Answers0