immayankmodi
immayankmodi

Reputation: 8580

storing image to byte[] into Mysql using asp.net and c#

I am using Asp.net with C# and back-end MySql to keep Images as byte[] array with using BLOB datatype

TABLE : ImageLog

ImgID                 int (auto increment)
ImageLogo             blob 

I am using following function to convert image to array...

private byte[] ConvertImageToByteArray(FileUpload fuImgToByte)
    {
        byte[] ImageByteArray;
        try
        {
            MemoryStream ms = new MemoryStream(fuImgToByte.FileBytes);
            ImageByteArray = ms.ToArray();
            return ImageByteArray;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

here is calling method for creating byte[] bt to insert into MySql

Byte[] bt = null;
bt = ConvertImageToByteArray(FileUploader1); --> Passing File Uploader ControlID

inserting like...

INSERT INTO IMAGELOG (ImageLogo) VALUES ('"+bt+"');

Now, Program runs perfectlly without causing any errors but when image stored into MySql, it stored like System.Byte[] not into byte[] array. Result Something like this...

ImgID      ImageLogo
________________________________
  1        System.Byte[]    13K ( Length )  < ----- > not storing byte[] in proper format
  2        System.Byte[]    13K ( Length )

Please tell me is it in proper format ? ? or not ?? Every suggestions are welcome. Thanks in advance

Upvotes: 0

Views: 5896

Answers (1)

immayankmodi
immayankmodi

Reputation: 8580

Problem Solved After Lot's of Difficulties... Simply Add Parameters With ? instead passing byte array bt directly within Insert Query...Something like this:

INSERT INTO IMAGELOG (ImageLogo) VALUES (?p1) and Pass values something like this

cmd.Parameters.Add("?p1", bt); <-- Adding parameter p1 value here

Note: If you are using MySql as database end then i suggest to use ? instead @ symbol.

OUTPUT:

ImgID      ImageLogo
________________________________
  1        Binary Image    73K ( Length ) < ----- > You can see the difference...
  2        Binary Image    69K ( Length )

Hope It Helps You All, Chears. !!

Upvotes: 1

Related Questions