Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
when i try to save the image in sql it shows $%# this type of data in column of image.

What I have tried:

i try alot but enable to do it.
Updated 28-Jun-20 23:02pm
Richard MacCutchan 29-Jun-20 5:01am
You need to provide much more information about how you are saving the image, and how you are retrieving it from the database.
Member 14552976 29-Jun-20 5:06am
i am retrieving the image in mvc application.
RedDk 30-Jun-20 23:09pm
I always try ALL OF THEM (varbinary, nvarchar, varchar, blob, etc) then when I find NOTHING works, rethink th eoriginal problem. As stated beneath here in Solution 1, you're trying to tilt the untiltable if what you think is yeah-wide (bytes) turns out to be yeah-wide-x-2/etc. Like trying to guess what the x and y of a specific image file in it's RAW state by testing every dimensional possibility.

1 solution

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

Solution 1

Don't try to save images in NVARCHAR (or VARCHAR) columns: they are for text, not binary data. Instead use a VARBINARY column, or convert your image to Base64 and store that - be aware that you will both need a lot more space to do that, and need to convert it back before you can use it. VARBINARY is a better option.
Member 14552976 29-Jun-20 5:05am
if i try to save it in varbianary it gives error when i try to run the mvc application which gets the data in string form.
OriginalGriff 29-Jun-20 5:13am
Then you need to look at your MVC application and see what it is doing with the data - if it's expecting binary data in a text field, then it's in for a corrupt image shock: treating it as Unicode data will really mess up images as it gets translated between UTF-8 and UTF-16 via characters that don't exist ...

It may be expecting a text version of the image - Base64 is common - but then it will "decode" the data to a "proper" image and you need to know how it does that in order to save it in the same format.
Member 14552976 29-Jun-20 5:16am
now i want to save it in nvarchar datatype because i am retrieving it as a string on controller side .if i dont do that it will result in errors.
OriginalGriff 29-Jun-20 5:42am
No. As I explained, you need to check what the other end is doing and do something complimentary or you will just give yourself even more problems.
Member 14552976 30-Jun-20 3:05am
does there is any way to save it ai nvarchar
OriginalGriff 30-Jun-20 3:25am
I told you that you would need to convert it to a text format such as Base64, but the important thing is to find out what the consuming MVC app is doing with it, what it expects.
If it expects Base64, then provide that. If it expects raw binary, then your table should contain raw binary.
Member 14552976 30-Jun-20 3:27am
it expects image as a string,if i save it in varbinary it wont accept it.
OriginalGriff 30-Jun-20 4:13am
I shall count to ten ... and then bang my head on the desk for a while, because it is lovely when you stop ...

Are all strings an image? No. Some are text like this.
So the MVC app must do something with the string to change it to or use it as a string. Or at least to check it is a "string containing an image" rather than a "string containing the worls of Shakespear".

And it's kinda important that whatever it expects to do with it, you provide the data in a form that it can cope with. Yes, that's a string. But there are many, many ways to make a "string that contains an image" and we have no idea what it is doing. And clearly, right now neither do you. And it's kinda important that you find out what it'd doing, what it expects, because other wise it's all going to be a waste of time.

Now, I can't do that for you, can I? So as I said a day ago: "you need to look at your MVC application and see what it is doing with the data"

And I will go and get a couple of Aspirin - my head hurts and there is a dent in my desk.

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