Solved

C# Using Database column as part of the Image name

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

680 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