title: Problems converting image to bytes and then to string
body: <p>I have a socket communication based application and the server sends a request to a client for a signature image. The signature is taken at the client and sent back to the server.</p>
  
  <p>To do this I have used a picture box, who's data is converted into bytes and then into a string to send over the socket connection to the server. This all works OK.</p>
  
  <p>I am having problems saving the data received in the database because I need to be able to save it as an image type but at this point the data is still a string so I am getting the error:</p>
  
  <p>"Operand type clash: nvarchar is incompatible with image"</p>
  
  <p>I presume it's because I am trying to send a string to a sql image type?</p>
  
  <p>Code which attempts to call the procedure and errors (server end- received data as string):</p>
  
  <pre><code>try
  {
      using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GetConnector"].ConnectionString))
      {
          SqlCommand sqlComm = new SqlCommand("PL_Device_ReadData", conn);
          sqlComm.Parameters.AddWithValue("@DeviceTypeID", DeviceID);
          sqlComm.Parameters.AddWithValue("@RequestID", RequestID);
          if (DeviceID != "5")
          {
              sqlComm.Parameters.AddWithValue("@Reading", Reading);
          }
          else if (DeviceID == "5")
          {
              sqlComm.Parameters.AddWithValue("@ImageReading", Reading);
              sqlComm.Parameters.Add("@ImageReading", SqlDbType.Image);
              sqlComm.Parameters["@ImageReading"].Value = Reading;
          }
          sqlComm.CommandType = CommandType.StoredProcedure;
  
          SqlDataAdapter da = new SqlDataAdapter();
          da.SelectCommand = sqlComm;
  
          da.Fill(ds);
      }
  }
  
  catch (Exception e)
  {
  
  }
  </code></pre>
  
  <p>Stored Procedure:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[PL_Device_ReadData]
      @DeviceTypeID INT,
      @RequestID INT,
      @Reading NVARCHAR(100) = NULL,
      @ImageReading IMAGE = NULL  
  AS
  BEGIN
  --Data
      IF @DeviceTypeID = 5 
      BEGIN
          UPDATE dbo.DeviceRequests 
          SET ImageData = @ImageReading
          WHERE id = @RequestID
          AND DeviceTypeID = @DeviceTypeID
      END
      ELSE
      BEGIN   
          UPDATE dbo.DeviceRequests 
          SET Value = @Reading
          WHERE id = @RequestID
          AND DeviceTypeID = @DeviceTypeID
  END 
  END
  </code></pre>
  
  <p>Converting the image into bytes:</p>
  
  <pre><code>byte[] imgData = null;
  // storage for the img bytes
  imgData = ImgToByteArray(picSignature.InitialImage, ImageFormat.Jpeg);
  
  public static byte[] ImgToByteArray(Image img, ImageFormat imgFormat)
  {
      byte[] tmpData = null;
      using (MemoryStream ms = new MemoryStream())
      {
          img.Save(ms, imgFormat);
  
          tmpData = ms.ToArray();
      }
      // dispose of memstream
      return tmpData;
  
  }
  </code></pre>
  
  <p>Converting the bytes to string (to send over socket):</p>
  
  <pre><code>_Reading = System.Text.Encoding.UTF8.GetString(_imagevalues);
  </code></pre>