Skip to content Skip to sidebar Skip to footer

Can't Save Byte[] Array Data To Database In C#. It's Saving 0x

I am trying to upload a file and save it's binary content to my database field. Each time I try to save, it simply enters the database as 0x. Here is the relevant code:

Solution 1:

I was having the same problem, that is, the first time the image was saved correctly on the database side, but if subsequently validation failed and then I tried to save the image again after entering valid data I would get 0x in the image column. To solve that I did what @Ann L. said:

byte[] photo = null;

if(model.Photo != null)
{
    var stream = model.Photo.InputStream;
    stream.Position = 0;

    using(BinaryReader br = new BinaryReader(model.Photo.InputStream))
    {
        photo = br.ReadBytes(model.Photo.ContentLength);
    }
}

Post a Comment for "Can't Save Byte[] Array Data To Database In C#. It's Saving 0x"