Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET Image visble

Posted on 2012-03-21
15
Medium Priority
?
266 Views
Last Modified: 2012-08-13
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
Comment
Question by:hayward03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
15 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37746094
<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
 
LVL 9

Expert Comment

by:sarabhai
ID: 37746367
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
 

Author Comment

by:hayward03
ID: 37746386
Hi,

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

Regards

Michael
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37746425
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
 

Author Comment

by:hayward03
ID: 37746452
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37746478
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
 

Author Comment

by:hayward03
ID: 37746498
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
 

Author Comment

by:hayward03
ID: 37746505
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
 
LVL 9

Expert Comment

by:sarabhai
ID: 37746869
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37746894
Try This:-


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

Author Comment

by:hayward03
ID: 37747023
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
 

Author Comment

by:hayward03
ID: 37747029
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747055
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
 

Accepted Solution

by:
hayward03 earned 0 total points
ID: 37747071
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
 

Author Closing Comment

by:hayward03
ID: 37764916
It worked and did exactly what I was wanting it to do.
0

Featured Post

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.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

609 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