Link to home
Create AccountLog in
Avatar of Sirdots

asked on

Showing Images within a datalist control

I have this method that displays information
on a datalist from my database.
Testing is the path where my pictures exists.
presently my pictures does not show at all. How
can I write this within my Item template to make
this pictures show?

Here is my datalist Itemtemplate
        <div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
          <asp:Image ID="Image1" runat="server" />
          <b>Custid: </b><%# DataBinder.Eval(Container.DataItem, "custid") %><br>
          <b>fname: </b><%# DataBinder.Eval(Container.DataItem, "fname") %><br>
          <b>lname: </b><%# DataBinder.Eval(Container.DataItem, "lname") %><br>
          <b>address: </b><%# DataBinder.Eval(Container.DataItem, "address") %><p>
Here is my code behind.
 private void ItemsGet()
            SqlConnection con;
            SqlDataAdapter dladpt;
            DataSet dldst;
            con = new SqlConnection(@"Data Source=zombie;Initial Catalog=pubs;Integrated Security=True");
            dladpt = new SqlDataAdapter("select custid, fname,lname,address, 'C:\\directoryPictures\\'+ convert(varchar(10),custid) + '.jpg' as testing from dotstest", con);
            dldst = new DataSet();
            dladpt.Fill(dldst, "Title");
            PagedDataSource objpds = new PagedDataSource();
            objpds.DataSource = dldst.Tables[0].DefaultView;      
            objpds.AllowPaging = true;
            objpds.PageSize = 2;
            objpds.CurrentPageIndex = CurrentPage;
            lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
            cmdPrev.Enabled = !objpds.IsFirstPage;
            cmdNext.Enabled = !objpds.IsLastPage;
            MyDataList.DataSource = objpds;

Open in new window

Avatar of Jeeva Subburaj
Jeeva Subburaj
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Sirdots


Thanks for your response. Do you know how I can achieve this with the way I am writing my code via the database i.e getting the path.

Avatar of Sirdots


I am wondering how I can use the server.mappath to get the virtual path. Thanks again
create a subfolder in your project location and use server.mappath(".\\IMAGE_SUB_FOLDER_NAME")

Avatar of Sirdots


Thanks. I am not sure you looked at this part of my code.
select custid, fname,lname,address, 'C:\\directoryPictures\\'+ convert(varchar(10),custid) + '.jpg' as testing from dotstest", con)

I am building the path and returning it with the other content of the sql statement as a datatable. So how do I use server.mappath within a select statement ?

in the select statement , you need not mention the physical path.. let it juz return the image file name only .

select custid, fname,lname,address, 'convert(varchar(10),custid) + '.jpg' as testing from dotstest

and in ur IMAGE URL, put it like

<asp:Image ID="Image1" runat="server" ImageUrl='SUB_FOLDER_NAME\<%# DataBinder.Eval(Container.DataItem, "testing") %>'/>
Avatar of Sirdots


Thanks again. I followed what you said.
i used this :
<asp:Image ID="Image1" runat="server" ImageUrl='directoryPictures\<%# DataBinder.Eval(Container.DataItem, "testing") %>' Width="87px" Height="106px"/>

when I view the page view source, this is what I get.


It is still not working.
put the Front Slash instead of backslah in 'SUB_FOLDER_NAME\

ImageUrl='SUB_FOLDER_NAME/<%# DataBinder.Eval(Container.DataItem, "testing") %>'/>
Avatar of Sirdots


I did exactly the same thing but still doesnt work.

 <asp:Image ID="Image1" runat="server" ImageUrl= 'directoryPictures/<%# DataBinder.Eval(Container.DataItem, "testing") %>' Width="87px" Height="106px"/>
try this

ImageUrl='<%#Databinder.Eval(Container.DataItem, "testing", "/subfoldername/{0}") %>'
Avatar of Sirdots


Now I am getting an error with this change. Here is the error below

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'Databinder' does not exist in the current context

Source Error:
Line 27:            
Line 28:         <div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
Line 29:           <asp:Image ID="Image1" runat="server" ImageUrl= '<%#Databinder.Eval(Container.DataItem, "testing", "/directoryPictures/{0}") %>'  Width="87px" Height="106px"/>
Line 30:           <b>Custid: </b><%# DataBinder.Eval(Container.DataItem, "custid") %><br>
Line 31:           <b>fname: </b><%# DataBinder.Eval(Container.DataItem, "fname") %><br>
Avatar of Sirdots


I just discovered that the first solution was wrong but was using the wrong extension for my images. Thanks for your help