Solved

C# Using Database column as part of the Image name

Posted on 2013-01-07
4
229 Views
Last Modified: 2013-01-08
Hi

I'm trying to learn c# but from a classic asp background, I get very confused (easily)

I am trying to fetch and dispay an image. All I need from the database is the productid the rest of the url will be hard coded like this

"http://www.example.com/" + <%# MyProductid %> + ".jpg"


 <asp:Image ID="Image1" runat="server" Width="200" Height="200" ImageUrl='<%# "http://www.example.com/" + <%# MyProductid %> + ".jpg" %>' />

but that does not work.

it does work for things like title
<title><%= MyTitle %></title>

my data looks like this

public partial class _Default : System.Web.UI.Page 
{
    private string myTitle;
    protected string MyTitle
    {
        get { return myTitle; }
        set { myTitle = value; }
    }
    private string myHeading;
    protected string MyHeading
    {
        get { return myHeading; }
        set { myHeading = value; }
    }
    private string myProductinfo;
    protected string MyProductinfo
    {
        get { return myProductinfo; }
        set { myProductinfo = value; }
    }
    private string myProductid;
    protected string MyProductid
    {
        get { return myProductid; }
        set { myProductid = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        String ConnStr = "";
        String SQL = "SELECT a.productname,a.productid,b.description,a.tag,a.modelnumber FROM table a join be_posts b on a.productid = b.productid WHERE a.productid ='20130'";
        SqlDataAdapter TitlesAdpt = new SqlDataAdapter(SQL, ConnStr);
        DataSet Titles = new DataSet();
        TitlesAdpt.Fill(Titles);
        DataRow dr = Titles.Tables[0].Rows[0];
        dr["productname"].ToString();
        MyTitle = dr["productname"].ToString();
        myHeading = dr["productname"].ToString();
        myProductinfo = dr["description"].ToString();
        myProductid = dr["productid"].ToString();
        
    }
}

Open in new window


please can someone show me an example of how to do this properly?
0
Comment
Question by:jasont09
  • 2
4 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38753548
Is this a typo or is this what you have written?
<%# MyProductid %> 

Open in new window


Does changing it to the below help?
<%= MyProductid %> 

Open in new window

0
 

Author Comment

by:jasont09
ID: 38753696
thanks

I changed it as above to

<asp:Image ID="Image1" runat="server" Width="200" Height="200" ImageUrl='"http://www.example.com/" + <%= MyProductid %> + ".jpg"' />

but the output comes outs as

<img id="Image1" src="&quot;http://www.example.com/&quot;%20+%20&lt;%=%20MyProductid%20%>%20+%20&quot;.jpg&quot;" style="height:200px;width:200px;border-width:0px;" />
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 38753923
<asp:Image ID="Image1" runat="server" 
         ImageUrl='<%# "http://www.example.com/" + Eval("MyProductid ") %> + ".jpg"'' />

Open in new window

See this tutorial
http://www.ezzylearning.com/tutorial.aspx?tid=1399196
Or if the control is not in Grid or Datalist, then you can simply add by code behind
Image1.ImageUrl="http://www.example.com/"+ dr["MyProductid "].ToString() +".jpg";

Open in new window

0
 

Author Closing Comment

by:jasont09
ID: 38754355
Thanks

This was the one

Image1.ImageUrl="http://www.example.com/"+ dr["MyProductid "].ToString() +".jpg";

I kept reading about Eval("") but did not realise it only works with grid and list. I am using datatable which explains it.

Many thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing string in C# 3 33
getting id from database 5 24
Asp.net mvc 5 5 19
Cannot upload files above 1mb IIS7 11 15
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

18 Experts available now in Live!

Get 1:1 Help Now