locked
saving and retrieving images sql server 2005 RRS feed

  • Question

  • hi guys,

    i want to save and retieve images into sql server 2005. i have a table caled resourcephotos with the columns:

    res_id(int)   and res_photo(image).

    i don't have problems with saving the imagen into the database...when a new image is saved, then u see in the res_photo column <binary data>

    bud when i want to select the image and set it to the picturebox i get the error: parameter is not valid

    here some code of my app to retrieve image and set it into the picturebox:

    SqlCommand cmd27 = new SqlCommand("SELECT Res_photo FROM ResourcePhotos WHERE Res_id = '" + res_id + "'", myconn);

    byte[] b = (byte[])cmd27.ExecuteScalar();

    MemoryStream myms = new MemoryStream(b);

    Image img = _res.pictureBox1.Image;

    img = Image.FromStream(myms);         <---------------------------------HERE COMES THE ERROR

    _res.pictureBox1.Image = img;

     

    i also tried Bitmap instead of Image, bud i got the same error.

    does anyone any idea? thanx.

    Wednesday, October 10, 2007 1:56 PM

Answers

  • Hi Djavid,

    passing binary data as string will not work.

    Try something like the following:

    Code Block

    ...

    ImageConverter c = new ImageConverter();

         byte[] imgA = (byte[])c.ConvertTo(pictureBox1.Image, typeof(byte[]));

    ...

        

         mycommPic.CommandText = String.Format("Update ResourcePhotos SET Res_photo=@resImg, Res_filename ='{0}' Where Res_id = {1}", res_pic_name, res_id);


         mycommPic.Parameters.Add(new SqlParameter("newImg", System.Data.SqlDbType.Image));
         mycommPic.Parameters["newImg"].Value = imgA;
         mycommPic.ExecuteNonQuery();

    ...


    Wednesday, October 10, 2007 6:15 PM

All replies

  • Hi Djavid,

    it seems to me, the error you get has nothing to do with your code. Somehow, the image format must have been mixed up on the way to the database.

    How did you save it? And which db column type have you set?


    Regards,
    franking

    Wednesday, October 10, 2007 5:12 PM
  • Hi Franking,

    thank you for your response.

    the db clumn type is IMAGE.

    here is some code of saving the image into the db:

    MemoryStream mstr = new MemoryStream();

    pictureBox1.Image.Save(mstr, pictureBox1.Image.RawFormat);

    byte[] res_arrimage = mstr.GetBuffer();

     

    SqlCommand mycommPic = new SqlCommand();

    mycommPic.CommandText = "Update ResourcePhotos SET Res_photo='" + res_arrimage + "'" +

    ",Res_filename ='"+res_pic_name+"' Where Res_id = '" + res_id + "'";

    mycommPic.Connection = myconn;

     

     

    myconn.Open();

    mycommPic.ExecuteNonQuery();

     

    i did'nt paste here everything of ADO, bud it seems that it works, cause when i open the db table, i see in the Res_filename column the image filename and in the Res_photo column i see: <Binary data> .

    so i still don't no what the problem is.

    Wednesday, October 10, 2007 6:00 PM
  • Hi Djavid,

    passing binary data as string will not work.

    Try something like the following:

    Code Block

    ...

    ImageConverter c = new ImageConverter();

         byte[] imgA = (byte[])c.ConvertTo(pictureBox1.Image, typeof(byte[]));

    ...

        

         mycommPic.CommandText = String.Format("Update ResourcePhotos SET Res_photo=@resImg, Res_filename ='{0}' Where Res_id = {1}", res_pic_name, res_id);


         mycommPic.Parameters.Add(new SqlParameter("newImg", System.Data.SqlDbType.Image));
         mycommPic.Parameters["newImg"].Value = imgA;
         mycommPic.ExecuteNonQuery();

    ...


    Wednesday, October 10, 2007 6:15 PM
  • Thank you,

     

    I was also facing the same problem. 

     

    This code helped me a lot.

     

     

    Dinesh Gupta  

    Friday, November 16, 2007 4:44 AM