TheNewone
TheNewone

Reputation: 97

How to not eval when System.DBnull?

I have a gridview shoving spareparts width image on only some of them. In my gridview i have this itemtemplate

<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" Height="100px" Width="100px"  ImageUrl='<%#  
"data:Image/png;base64,"+ Convert.ToBase64String((byte[])Eval("Img")) %>' 
runat="server" />                                                           
</ItemTemplate>
</asp:TemplateField>

This get me an error "System.dbnull when there is no image in the database. i ave tried a lot of things, but i dont' want to post them here. They dont' work I do not like the idea to insert an default image to every rows. also i would like the size to only be whats necessary. How do i solve this problem?

Upvotes: 1

Views: 454

Answers (3)

Zeek2
Zeek2

Reputation: 426

@Ray H's solution eventually worked for me but, because our code-behind is in VB rather than C#, it requires different mark-up! In the hope that it might help others, here is my conversion of Ray's solution for VB users - it's a bit more long-winded:

               <asp:TemplateField HeaderText="Image" >
                        <ItemTemplate>
                            <asp:Image ID="Image2" runat="server" Width="100px" Height="42px" ImageUrl='<%#If(Eval("Image") IsNot DBNull.Value, "data:Image/png;base64," + Convert.ToBase64String(Eval("Image")), String.Empty)  %>' />
                        </ItemTemplate>
                        <ItemStyle Font-Names="9px" HorizontalAlign="Left" />
                    </asp:TemplateField>

The key section being this:

ImageUrl='<%#If(Eval("Image") IsNot DBNull.Value, "data:Image/png;base64," + Convert.ToBase64String(Eval("Image")), String.Empty)  %>' />

Upvotes: 0

Ray H
Ray H

Reputation: 521

you can use Eval(“Image”) is DBNull to judge what content to output i did not use webform for long, but should work

<asp:TemplateField HeaderText="Image">
<ItemTemplate>


    <asp:Image 
Visible='<%#Eval("Img")!=DBNull.Value%>' // hide the image if no data 
ID="Image1" Height="100px" Width="100px"  
ImageUrl='<%#Eval("Img")!=DBNull.Value ? "data:Image/png;base64,"+ Convert.ToBase64String((byte[])Eval("Img")) : string.Empty %>' 
    runat="server" />  

</ItemTemplate>
</asp:TemplateField>

Upvotes: 2

Win
Win

Reputation: 62260

It seems like you are rending images which have several kilobytes in size. If so, you should not render image as base64 encoded string.

The main reason is browser could not cache images, if they are embedded inside a page as base64 encoded string. As the result, every time a user views the same images, the browser will have to reload each and every images along with the page content.

Ideally, you should use image handler to render those images. It seems a lot at first, but trust me it'll dramatically prove the speed of the subsequent page load.

For example,

<asp:TemplateField HeaderText="Image">
    <ItemTemplate>
        <asp:Image ID="Image1" 
            ImageUrl='<%# "~/ImageHandler.ashx?id=" + Eval("Id") %>'
            runat="server" />                                                           
    </ItemTemplate>
</asp:TemplateField>

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var id = context.Request.QueryString["id"];    
        // Retrieve the image data as byte array from database based on the id    
        context.Response.BinaryWrite(YourImageByte);
    }

    public bool IsReusable { get{ return false; }}
}

Upvotes: 0

Related Questions