0

I want to save an image from my C# form application to SQL Server Database, currently i m doing this: In class library class named PhotoCL:

public void AddPhoto(int id, Image  photo)
    {
        db.ExecuteNonQuery("spAddPhoto", new object[] { id, photo});
    }

"spAddPhoto" is the stored Procedure which have 2 parameters, @id int, @img image In database the table of photos also have two columns with same data types as mentioned above in SP.

This is the Code behind my AddPhoto Button:

private void button1_Click(object sender, EventArgs e)
    {
        mp.AddPhoto(Convert.ToInt32(comboBox1.SelectedValue),pictureBox1.Image);
    }

I m getting this error at runtime(Debugging):

"Failed to convert parameter value from a Bitmap to a Byte[]."

Please help me out.. Thanks in advance..

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Do you absolutely positively need to save images to the database? – Arran Apr 03 '13 at 11:44
  • Why make it a mystery novel and leave the interesting code out? What is `db.ExecuteNonQuery` doing? – Remus Rusanu Apr 03 '13 at 11:46
  • Convert the image to byte first. see http://stackoverflow.com/questions/8880213/saving-an-image-file-to-sql-server-and-converting-byte-array-into-image?rq=1 – lionheart Apr 03 '13 at 11:49
  • @Remus you can say it as Connection String. I write this code in the begining of every of my Class Library's Class. Database db = DatabaseFactory.CreateDatabase("ConStr"); ConStr is the Connection string of database which is written in an Application Configuration File. – Umer Ikhlas Apr 03 '13 at 12:03
  • I m using the Classes of Enterprise Library. – Umer Ikhlas Apr 03 '13 at 12:11
  • Please Correct my Code :( anyone please. . – Umer Ikhlas Apr 03 '13 at 12:14

1 Answers1

0

You don't insert an image like this to a database, you should insert a flow of byte corresponding to the image.

Would this article be of help ?

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • Please Correct my Code :( anyone please. . – Umer Ikhlas Apr 03 '13 at 12:12
  • You're giving us 2 lines of code... it's not correction you need, it's coding. Maybe also this article can help ? http://www.shabdar.org/sql-server/105-store-save-images-in-sql-server.html – Laurent S. Apr 03 '13 at 12:41