0

I have a web application which allows e-signature. When I get the signature, it is the exact size(mostly greater than 8000 bytes).But when it goes to the database, it gets truncated to 8000 bytes all the time. When I checked varbinary, it says 'Implicit conversion fails if the byte array is greater than 8,000 bytes. Explicitly set the object when working with byte arrays larger than 8,000 bytes.' How do I do that in the following scenario? I don't want to loose any bytes.

public DataSet InsertInformation(FormInformation info)
{
    SqlCommand command = new SqlCommand("SPInsertSig");
    command.Parameters.Add("@Signature", SqlDbType.VarBinary).Value = info.Signature; 
    DataSet resultDataSet;
    resultDataSet = ExecuteStoreProcedure(command);
    return resultDataSet;
}

When I googled, someone mentioned trying this way but it doesn't work either and truncates the value to 8000 bytes.

SqlParameter param = command.Parameters.Add("@Signature", SqlDbType.VarBinary);
param.Value = info.Signature;
param.Size = -1;

Any suggestions will be really appreciated. Thanks

Update After changing it to Int32.MaxValue, it worked.

user2908229
  • 265
  • 1
  • 2
  • 11
  • 3
    use data type VARBINARY(MAX), also have a look at this [`Question`](http://stackoverflow.com/questions/5824620/what-sqldbtype-maps-to-varbinarymax) – M.Ali Nov 24 '14 at 21:56
  • My data type is already varbinary(max) and I believe the code I pasted above is truncating the size somehow. – user2908229 Nov 24 '14 at 22:11
  • What is the .NET data type of info.Signature? I would expect the second code snippet (Size -1) to work with a byte array. – Dan Guzman Nov 25 '14 at 01:07

2 Answers2

2

try to use a new SqlParemeter c'tor:

SqlParameter(String, SqlDbType, Int32)

or in your case:

command.Parameters.Add(new SqlParameter("@Signature", SqlDbType.VarBinary, -1)).Value = info.Signature; 
ymz
  • 6,602
  • 1
  • 20
  • 39
1

No one can definitively give you an answer of exactly what it is because of the lack of depth in the question, so you really need to follow a checklist to determine the issue. This is the order you should check in my opinion; most common reasons to least common in my experience. My bet in on #4 being the culprit due to the message you are seeing.

  1. Is the field in the table declared as varbinary(max)?
  2. Is the SQL Parameter in code sized correctly (i.e. .size=-1)?
  3. Does the stored procedure being called have the field sized correctly in the parameter declaration?
  4. Is there a CAST or CONVERT call specifying VARBINARY, but not the correct size in the stored procedure?
  5. Is there a trigger on the table that makes a backup copy of the data to another table? If so, is that table sized correctly? Also if there is a trigger, is there a CAST or CONVERT call specifying VARBINARY, but not the correct size in the stored procedure?
  6. Are there any places in the stored procedure where the original value is copied into a different variable and that variable is not sized correctly?
  7. Is there a view that is indexed against the field that might be declared incorrectly?
UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51