Click here to Skip to main content
14,390,428 members
Rate this:
Please Sign up or sign in to vote.
See more:
im getting error on line
ImageByte = comm.ExecuteScalar();

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


What I have tried:

byte[] ImageByte = null;
MemoryStream MemStream = null;
PictureBox PicBx = new PictureBox();
object OB;

string WorkingDirectory = Application.StartupPath + "\\";
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb; Persist Security Info=True";
cnction = new OleDbConnection(connString);
cnction.Open();
int ImageID = 6;
sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";
comm = new OleDbCommand(sqlCommand, cnction);
ImageByte = comm.ExecuteScalar();
MemStream = new MemoryStream(ImageByte);
PicBx.Image = Image.FromStream(MemStream);
}
Posted
Updated 26-Oct-19 10:01am
v2
Comments
Richard Deeming 29-Oct-19 12:39pm
   
sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";

Don't do it like that!

Whilst in this specific instance, since the parameter is an int, you've avoided a SQL Injection[^] vulnerability, building your queries like that is still a very bad habit to get into.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

const string sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = ?";
using (OleDbCommand comm = new OleDbCommand(sqlCommand, cnction))
{
    comm.Parameters.AddWithValue("ImageID", ImageID);
    byte[] imageBytes = (byte[])comm.ExecuteScalar();
    MemoryStream ms = new MemoryStream(imageBytes);
    PicBx.Image = Image.FromStream(ms);
}

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It means that your ImageObject field in your database is another type than expected.
To know how to define the field and read it, see example here: Reading and writing images to MS-Access 2007 and higher databases in C#, VB.NET for Visual Studio 2017[^]

In particular note:
Quote:
If you truly have to store images inside of an MS-Access database table be forewarned that with MS-Access 2007 and higher adding an image to a record from within MS-Access will not only store the image but also a header
   
v2
Comments
Richard Deeming 29-Oct-19 12:41pm
   
Actually, I don't think that's the problem here. ExecuteScalar returns an object. Whilst VB would let you assign that to a byte array variable, C# requires a cast:
ImageByte = (byte[])comm.ExecuteScalar();
RickZeeland 29-Oct-19 14:11pm
   
That seems like the right solution, bravo !

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100