Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Syntax problem in small conditional statement

Posted on 2012-09-07
Medium Priority
Last Modified: 2012-09-07
I have a listview with an image control.
I store the path to the image in a sql server db.
I need to evaluate the path to make sure it isn't null.  If it is null I need to change images.
I have tried multiple combinations trying to get this to work with no luck.
I either receive an error that the ? cannot be used or an expression is expected.

 <asp:Image ID="imgLeft" ImageUrl='<%#Eval("BeforePicPath") == null ? Eval("BeforePicPath").ToString() : "../images2020/resume-photo.jpg" %>' runat="server" />

Open in new window

What am I doing wrong?

Question by:Sheritlw
  • 3
  • 2
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38377177
try this

<%# DataBinder.Eval(Container.DataItem,"Col_3") == DbNull.Value ? (string)DataBinder.Eval(Container.DataItem, "Col_2") : (string)DataBinder.Eval(Container.DataItem, "Col_3") %

Open in new window

LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38377232
This worked For me with my dataset .
//C# Code
Text='<%# DataBinder.Eval(Container.DataItem,"id") == "" ? (string)DataBinder.Eval(Container.DataItem, "name") : (string)DataBinder.Eval(Container.DataItem, "id") %>'>

Open in new window


Author Comment

ID: 38377240
The alternative image is not referenced in the database, it is in a folder, so I tried...

 <asp:Image ID="imgLeft" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"BeforePicPath") == DbNull.Value ? "../images2020/resume-photo.jpg" : DataBinder.Eval(Container.DataItem,"BeforePicPath") %>'  runat="server" />

Open in new window

and received the same error -  BC30201: Expression expected.

LVL 18

Accepted Solution

Rajar Ahmed earned 2000 total points
ID: 38377288
try this,
<asp:Image ID="imgLeft" ImageUrl ='<%# IIf(DataBinder.Eval(Container.DataItem, "id") = DBNull.Value.ToString, "../images2020/resume-photo.jpg", DataBinder.Eval(Container.DataItem, "id"))%>' runat="server" />

Open in new window


Author Closing Comment

ID: 38377342
Thank you

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question