Solved

C# Using Database column as part of the Image name

Posted on 2013-01-07
4
231 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/"%20+%20<%=%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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

11 Experts available now in Live!

Get 1:1 Help Now