Click here to Skip to main content
14,735,552 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an Image type column in MSSQL database named photo. The name of database is also photo (I know thats bad).
I want to get this image and store it back in some other record.
Here is my code:
b.com.CommandText = "SELECT photo from photo WHERE model=@model";
b.com.Parameters.Add("@model", txt_model.Text.Trim());
if (b.con.State == ConnectionState.Closed)
b.con.Open();
b.dr = b.com.ExecuteReader();
if (b.dr.HasRows)
{
b.dr.Read();
b.com.Parameters.Add("@j", b.dr.GetValue(0));
b.dr.Close();
}

Here is my exception thrown by ExecuteScalar() when I tried to insert this image again.

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.


What I have tried:

Here is my code:
b.com.CommandText = "SELECT photo from photo WHERE model=@model";
b.com.Parameters.Add("@model", txt_model.Text.Trim());
if (b.con.State == ConnectionState.Closed)
b.con.Open();
b.dr = b.com.ExecuteReader();
if (b.dr.HasRows)
{
b.dr.Read();
b.com.Parameters.Add("@j", b.dr.GetValue(0));
b.dr.Close();
}

Here is my exception thrown by ExecuteScalar() when I tried to insert this image again.

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.
Posted
Updated 10-Jun-16 2:27am

1 solution

Probably, the query you don't show that throws the error is trying to return an image as it's scalar value - at which point you would get that error.
Without the actual code around the ExecuteScalar and the actual SQL it's impossible to tell, but they chances are that you are trying to do something like:
int i = (int) cmd.ExecuteScalar();
Which you can't do with a byte array.
Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] which gives the code I use.
   
Comments
Member 8057273 10-Jun-16 7:38am
   
I got the solution. I have to make a little change in the code placement and it worked. nyways, thanks @OriginalGriff for the try.

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