Error trying to save image in SQL Server

I created the code to upload the image to SQL Server.

Here is the code to convert the image to bytes:

//Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(p);         

//Open FileStream to read file
FileStream fStream = new FileStream(p, FileMode.Open, FileAccess.Read);
byte[] numBytes = new byte[fStream.Length];
fStream.Read(numBytes, 0, Convert.ToInt32(fStream.Length));

//Use BinaryReader to read file stream into byte array.

//BinaryReader br = new BinaryReader(fStream);

//When you use BinaryReader, you need to supply number of bytes to read from file.
//In this case we want to read entire file. So supplying total number of bytes.

// data = br.ReadBytes((int)numBytes);
return numBytes;

And here is the code to add bytes to the parameter SqlCommandas values:

 objCmd.Parameters.Add("@bill_Image", SqlDbType.Binary).Value = imageData;
  objCmd.ExecuteNonQuery();

But I get an error

String or binary data will be truncated. Statement completed

How can I solve this problem?

+5
source share
2 answers

The error clearly indicates that you are trying to store more bytes than allowed by the field definition.

You do not know what type of sql you are using for bill_Image, but there will be a suitable field definition for storing the image varbinary(MAX).

+4
source

Check the definition of the column bill_Imagein the database.

-

bill_Image varbinary(X)

X ( 8 000 )

/varbinary-

0

All Articles