Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
I want to retrieve a picture from an Access database using C# code of visual studio 2008.

I using the following code:
C#
private void load_picturebox_Click(object sender, EventArgs e)
{
    byte[] ImageByte = null;
    MemoryStream MemStream = null;
    PictureBox PicBx = new PictureBox();
    object OB;

    vcon.Open();

    int ImageID = 1;
    string sql = "SELECT picture FROM dictionary WHERE serial_no = " + ImageID + "";
    OleDbCommand vcom = new OleDbCommand(sql, vcon);

    ImageByte = vcom.ExecuteScalar();

    MemStream = new MemoryStream(ImageByte);
    PicBx.Image = Image.FromStream(MemStream);
}


But I couldn't run this code with the followed line: 
ImageByte = comm.ExecuteScalar(); 

The error is: "Cannot implicitly convert type 'object' to 'byte[]'. An explicit conversion exists (are you missing a cast?)"

Please, help me to solve the problem.
Posted

1 solution

The error you get tells exactly what is missing. ExecuteScalar returns an object and ImageByte is a byte array. Since a picture is a byte array you can make a explicit cast

ImageByte = (byte[]) vcom.ExecuteScalar();

should do the trick.

Beside, are you sure you want to use ExecuteScalar() because is returns a maximum of 2033 characters. With is pretty small for a picture

Piet
 
Share this answer
 
Comments
Member 8454009 21-Mar-12 13:21pm    
This is solved. But there is also have another error with the last followed line: PicBx.Image = Image.FromStream(MemStream);

The error is: Parameter is not valid.

I used Bitmap Image in my access database. But this don't show in picturebox of windowsform by the followed code.
Sergey Alexandrovich Kryukov 21-Mar-12 15:02pm    
Right, a 5.
--SA

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