locked
Cannot implicitly convert type 'byte[]' to 'string' RRS feed

  • Question

  • I want to upload an image in the database using the following method: 
    public void upload(byte[] pic, string name)
            {
                using (DataClasses1DataContext context = new DataClasses1DataContext())
                {
                    var result = (from usr in context.Tables
                                  where usr.name == name
                                  select usr).Single();
                    result.photo = pic;
                    context.SubmitChanges();
                }             
            }

    But I'm getting an error: "Cannot implicitly convert type 'byte[]' to 'string'".
    The field "photo" in the database table "Tables" has varchar(MAX) type, I also tried Image and varbinary(MAX) types. 
    How can I make this work?
    Thursday, July 10, 2014 6:59 PM

Answers

  • You have to convert your byte[] into Base64 string. Then you can assign that to 'result.photo'

    String picInString=Convert.ToBase64String(pic);

    result.photo=picInString;


    Srini

    • Marked as answer by Wizard_C Friday, July 11, 2014 5:27 PM
    Friday, July 11, 2014 9:36 AM