Solved

ASP.NET Image visble

Posted on 2012-03-21
15
262 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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