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

x
?
Solved

C# Using Database column as part of the Image name

Posted on 2013-01-07
4
Medium Priority
?
242 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
[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
  • 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…
Suggested Courses

604 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