Solved

ASP.NET Image visble

Posted on 2012-03-21
15
256 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now