0

I am trying to fetch image from database and i am getting this error

Unable to cast object of type 'System.String' to type 'System.Byte[]'.

here is my code

job.CompanyLogo = "data:Image/jpg;base64,"+ Convert.ToBase64String((byte[])rdr["CompanyLogo"]);

note. i have saved image in database in bytes

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • _"...i have saved image in database in bytes..."_, you do not say which ORM or data access tech you're using but I'd double check that. – Adriano Repetti Apr 18 '19 at 16:18
  • 2
    i would think rdr["CompanyLogo"] is coming as a string value and when you are trying to cast it to byte[] ..it is erroring out. – Ashok Apr 18 '19 at 16:53
  • You can get the string as a byte[] by using this `Encoding.UTF8.GetBytes(rdr["CompanyLogo"])` Just be sure to use the correct encoding for whatever your source string type is. – Matthew Brubaker Apr 18 '19 at 18:52

0 Answers0