Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am create windows application here i am using data grid view and display products images on picturebox but i am insert a data base in image error is came on below line

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

 command.Parameters["@imgmodel"].Value = pictureBox1.Image;



Please any one guide or help me.

What I have tried:

C#
Failed to convert parameter value from a Bitmap to a Byte[].
Posted
Updated 17-Dec-16 5:37am
Comments
[no name] 17-Dec-16 11:31am    
Convert the image to a byte array.
Boopalslm 17-Dec-16 11:32am    
how to convert give me some ideas.

You're lucky: most people mess up well before that and only find out once they have loaded the DB full of what they think are images, but aren't!

Same problem though, and same solution: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
 
Share this answer
 
Create a MemoryStream and save the Image to it, then you can convert the MemoryStream to a byte array and pass that to your SQL parameter:
C#
using (MemoryStream ms = new MemoryStream())
{
    pictureBox1.Image.Save(ms, ImageFormat.Png);
    command.Parameters["@imgmodel"].Value = ms.ToArray();
}
 
Share this answer
 
Comments
Boopalslm 17-Dec-16 11:43am    
error came on ImageFormat

The name ImageFormat does not exist in the current context how to solve this error
Boopalslm 17-Dec-16 11:48am    
I got it, thanks a lot for your guidance............Thanks a lot....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900