Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two image fields in my user entry and i want to save null to my database when ever the image is not provided but when retrieving the image to my gridview a error shows up
I want to know how to save a null image and retrieve it in gridview without any problem.

What I have tried:

<pre lang="c#">


<asp:TemplateField HeaderText="Photo">
                    <ItemTemplate>
                        <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# "data:Image/png;base64,"+Convert.ToBase64String((byte[])Eval("Photo")) %>' Height="50px" Width="50px" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID Copy">
                    <ItemTemplate>
                        <asp:Image ID="imgIDType" runat="server" ImageUrl='<%# "data:Image/png;base64,"+Convert.ToBase64String((byte[])Eval("IDCopy")) %>' Height="50px" Width="50px" />
                    </ItemTemplate>
                </asp:TemplateField>
Posted
Updated 14-Dec-17 6:07am

1 solution

Quote:
I want to know how to save a null image and retrieve it in gridview without any problem.
You can do that, by not storing anything; which you are doing. Secondly, NULL is the default value of a container, where you provide nothing.

If the content is null, then check for a null and "do not" render the image there, something like this would solve the problem and prevent the error,
C#
if(record.Image != null) {
    // Render the image
} else {
    // What fallback to use?
}

This way, you would be able to render the images only if they are available and skip them if they are not available there.

Since you want to do this in ASP.NET Web Forms controls, you would need to dynamically control when a control is rendered, and then render the control. This condition in the ASP.NET Web Forms would be something similar to the following code,
ASP.NET
<div runat="server" visible='<%# Eval("ImageUrl") != DBNull.Value %>'>
    <asp:Image runat="server" ImageUrl='<%#Eval("ImageUrl") %>' />
</div>

The code was taken from [here](https://stackoverflow.com/questions/47254842/asp-net-repeater-hide-image-controls-if-url-img-null-into-db-and-show-video-cont), change it to suit your requirement.

Lastly, I would also recommend that you do not store the images in the database. Instead try to store them in the file system and only store their names in the record.
 
Share this answer
 

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