Click here to Skip to main content
13,447,300 members (37,691 online)
Rate this:
Please Sign up or sign in to vote.
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">
                        <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# "data:Image/png;base64,"+Convert.ToBase64String((byte[])Eval("Photo")) %>' Height="50px" Width="50px" />
                <asp:TemplateField HeaderText="ID Copy">
                        <asp:Image ID="imgIDType" runat="server" ImageUrl='<%# "data:Image/png;base64,"+Convert.ToBase64String((byte[])Eval("IDCopy")) %>' Height="50px" Width="50px" />
Posted 14-Dec-17 4:52am
Updated 14-Dec-17 6:07am

1 solution

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

Solution 1

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,
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,
<div runat="server" visible='<%# Eval("ImageUrl") != DBNull.Value %>'>
    <asp:Image runat="server" ImageUrl='<%#Eval("ImageUrl") %>' />

The code was taken from [here](, 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180318.3 | Last Updated 14 Dec 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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