Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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
0
hayward03
Asked:
hayward03
  • 8
  • 5
  • 2
1 Solution
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
hayward03Author Commented:
It worked and did exactly what I was wanting it to do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 8
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now