ASP.NET Image visble

Hi,

Im recently new to ASP.NET C# and have a website that is showing images from a database and I only want to show the image if the value is set to true.

Database has fields Image1 - this is the image path and Image1_Show - Bit Value represent true or false

Then I have the code below

<a href='<%# "../images/cars/" + Eval("Image1") %>'><asp:Image ID="img1" Visible='<%# Eval("Image1_Show") %>' runat="server" Border="0" ImageUrl='<%# "../images/cars/thumbnails/small/" + Eval("Image1") %>' /></a>

But i keep getting the error: Specified cast is not valid.

I have been trying to get this to work for a few days so any help would be amazing

Regards

Michael
hayward03Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BuggyCoderCommented:
<a href='<%# "../images/cars/" + Eval("Image1") %>'><asp:Image ID="img1" Visible='<%# CheckImageStatus(Eval("Image1_Show")) %>' runat="server" Border="0" ImageUrl='<%# "../images/cars/thumbnails/small/" + Eval("Image1") %>' /></a>

///Create This Function to return true or false in code behind file
public bool CheckImageStatus(int status)
{
return status==1;
}

Open in new window

0
sarabhaiCommented:
function Image_OnError(image)
{
image.onerror = "";
image.style.visibility = "hidden";
return true;
}
add above java-script function to js file then add onerror to image tag.

<a href='<%# "../images/cars/" + Eval("Image1") %>'><asp:Image ID="img1" Visible='<%# Eval("Image1_Show") %>' runat="server" onerror="Image_OnError(this);" Border="0" ImageUrl='<%# "../images/cars/thumbnails/small/" + Eval("Image1") %>' /></a>
0
hayward03Author Commented:
Hi,

Thank you for your answers, but both of these didn't work when I tried.

Regards

Michael
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

BuggyCoderCommented:
please share the error, although have made modifications try the code given below.
Kindly debug and see what html is rendered on client side and share that as well.

<a href='<%# "../images/cars/" + Eval("Image1") %>'><asp:Image ID="img1" Visible='<%# CheckImageStatus(Eval("Image1_Show")) %>' runat="server" Border="0" ImageUrl='<%# "../images/cars/thumbnails/small/" + Eval("Image1") %>' /></a>

///Create This Function to return true or false in code behind file
public string CheckImageStatus(int status)
{
return status==1?"true":"false";
}

Open in new window

0
hayward03Author Commented:
Below is the error from the lastest code you supllied being used.

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for 'TrevorAttfield.Cars.CheckImageStatus(int)' has some invalid arguments

Source Error:
0
BuggyCoderCommented:
This means that bit value of Image1_Show is returning null.

Change the method as:-

       
public string CheckImageStatus(int? valImage)
        {
            return (valImage.HasValue && valImage.Value == 1 ? "true" : "false");
        }

Hopefully, we are getting either of the 3 values from DB in your Image1_Show field as :
1,0,null.
0
hayward03Author Commented:
Hi,

The error is below

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for 'TrevorAttfield.Cars.CheckImageStatus(int?)' has some invalid arguments

Source Error:


Here's database table design as well, currently Image1_Show is showing True.

ID      int      
Title      varchar(500)
Price      varchar(100)
Make      varchar(50)
Model      varchar(50)
Year      varchar(50)
Miles      varchar(100)
Transmission      varchar(100)
Engine      varchar(100)
Fuel      varchar(50)
Description      varchar(MAX)
MainImage      varchar(500)
Image1      varchar(500)
Image2      varchar(500)
Image3      varchar(500)
Image4      varchar(500)
Image5      varchar(500)
Image6      varchar(500)
Image1_Show      bit
Image2_Show      bit
0
hayward03Author Commented:
Hi,

Attached are the files that I'm working with.

Thanks for your help.

Regards

Michael
Cars.aspx
Cars.aspx.cs
Cars.aspx.designer.cs
0
sarabhaiCommented:
just try it and tell me where is the problem...
did u known where to add the java script function? add it first and then try the solution


function Image_OnError(image)
{
image.onerror = "";
image.style.visibility = "hidden";
return true;
}

add above java-script function to js file then add onerror to image tag.
in aspx file at your <image> tag should look like following

<a href='<%# "../images/cars/" + Eval("Image1") %>'><asp:Image ID="img1"  runat="server" onerror="Image_OnError(this);" Border="0" ImageUrl='<%# "../images/cars/thumbnails/small/" + Eval("Image1") %>' /></a>
0
BuggyCoderCommented:
Try This:-


public string CheckImageStatus(bool? valImage)
        {
            return (valImage.HasValue && valImage.Value ? "true" : "false");
        }
0
hayward03Author Commented:
Hi Sarabhai,

I have added your code and it doesnt work and also how is this getting the value from the DB???

Regards

Michael
0
hayward03Author Commented:
Hi BuggyCoder,

I have also tried your code and it produce the following error

CS1502: The best overloaded method match for 'TrevorAttfield.Cars.CheckImageStatus(bool?)' has some invalid arguments
0
BuggyCoderCommented:
could you please debug at Markup and see what does the Eval("Image1_Show") returns....
Check the data type and modify the method's parameter accordingly
0
hayward03Author Commented:
Hi,

Thank you both of you but I have managed to find some code online that worked

Visible='<%# (Eval("Image1")==DBNull.Value ? false : true) %>'

Thanks for all your help

Regards

Michael
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hayward03Author Commented:
It worked and did exactly what I was wanting it to do.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.