Click here to Skip to main content
14,494,314 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm having trouble trying to display images from MySql database with Medium Blob data type in ASP.Net C#

What I have tried:

I tried this but it displays nothing and no error message. I used to use this code below in SQL database (Microsoft Server Management Studio), it worked fine with VARCHAR MAX.

<asp:Image ID="prodImg" ImageUrl='<%#Eval("image") %>' class="prodImage" runat="server" />

Is there any way to display blob in the code behind instead? Thank you so much in advance!
Posted
Updated 26-Mar-20 1:22am
v3
Comments
OriginalGriff 26-Mar-20 6:32am
   
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
Cee08 26-Mar-20 7:15am
   
I'm sorry. It displays nothing and no error message. I used to use the code above in SQL database (Microsoft Server Management Studio), it worked fine with VARCHAR MAX.

1 solution

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

Solution 1

Look at the ImageUrl attribute. Normally this would be the URI of the image, but in your case you want to put the file contents in directly.

There are a few things that need to be done to get this to work:
1. You have to tell the image control that you are giving it data (prepend with "data:")
2. You need to tell the image control what type of image it is (eg "image/gif;")
3. You need to tell the image control the data format (eg: "base64,")
4. You need to give the image control the data in that format.

The content of that ImageUrl as a string would then look something like
string img = "data:image/gif;base64," + Convert.ToBase64String(<%#Eval("image"));
   
Comments
Richard Deeming 26-Mar-20 7:26am
   
Slight syntax error in your code:
ImageUrl='<%# "data:image/gif;base64," + Convert.ToBase64String((byte[])Eval("image")) %>'
Cee08 26-Mar-20 8:00am
   
Nothing happened sir :(
Cee08 26-Mar-20 8:00am
   
@MadMyche it says too many characters in character literals.
MadMyche 26-Mar-20 10:41am
   
Are you using single or double quotes?
Cee08 26-Mar-20 12:06pm
   
I tried your code above but it says struct System.Boolean
Represents a Boolean (true or false) value. Operator '<' should be preceded by white space. Operator should be followed by white space. Invalid term '<'

Then I added %>'

string img = "data:image/png;base64," + Convert.ToBase64String('<%#Eval("image") %>');

It says too many characters in character literals.

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