Solved

C# Using Database column as part of the Image name

Posted on 2013-01-07
4
237 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

740 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