Saving and retrieving image in SQL database from C# problem

Posted by Mobin on Stack Overflow See other posts from Stack Overflow or by Mobin
Published on 2010-05-17T21:59:28Z Indexed on 2010/05/17 22:00 UTC
Read the original article Hit count: 330

Filed under:
|
|
|

I used this code for inserting records in a person table in my DB

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                this.personTableAdapter1.Insert(NIC_Box.Text.Trim(), Application_Box.Text.Trim(), Name_Box.Text.Trim(), Father_Name_Box.Text.Trim(), DOB_TimePicker.Value.Date, Address_Box.Text.Trim(), City_Box.Text.Trim(), Country_Box.Text.Trim(), ms.GetBuffer());

but when i retrieve this with this code

    byte[] image = (byte[])Person_On_Application.Rows[0][8];
                        MemoryStream Stream = new MemoryStream();
                        Stream.Write(image, 0, image.Length);
                        Bitmap Display_Picture = new Bitmap(Stream);

Image_Box.Image = Display_Picture;

it works perfectly fine but if i update this with my own generated Query like

System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Image img = Image_Box.Image;
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                Query = "UPDATE Person SET Person_Image ='"+ms.GetBuffer()+"'  WHERE (Person_NIC = '"+NIC_Box.Text.Trim()+"')";

the next time i use the same code for retrieving the image and displaying it as used above . Program throws an exception

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql