Hi All

I am working since 2 days to display images in a datalist and the code used is given below, but its not displaying the images.
I am storing path of the image in database in imagepath cooumn. Kindly help me please.

html code

<asp:DataList ID="dlImage" runat="server" RepeatColumns="3" Width="311px" Height="334px">
        <ItemTemplate>
        <asp:ImageButton ID="imgPhotos" ImageUrl='<%#Eval("imagepath")%>' Height="100%" Width="100%" runat="server" />
        </ItemTemplate>
        </asp:DataList>

c# code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;


 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cnOledb = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + strRootDir + "App_Data\\dbPhotos.mdb");
        cnOledb.Open();
string strSelImages = "select imagepath from tblPhotos";
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(strSelImages, cnOledb);
        da.Fill(ds);
        dlImage.DataSource = ds.Tables[0];
        dlImage.DataBind();
        cnOledb.Close();
        }

    }

Thanks
wajid

Dani AI

Generated

As discovered, the browser needs a web URL for an image, not a server file path. An ASP.NET Image/ImageButton simply renders whatever string you bind into the generated HTML src attribute; if that string is a Windows path (backslashes, drive letter) the browser cannot request it. ’s question about binary storage is also worth noting: serving BLOBs is a different pattern (HTTP handler or streaming endpoint) than serving static files.

If the DB contains only filenames or relative paths, build a proper virtual URL before binding. Example pattern (code-behind) — add a new column with an application-relative URL and bind that column to the image control:

foreach (DataRow r in table.Rows) {
    string file = System.IO.Path.GetFileName(r["imagePath"].ToString());
    r["virtualUrl"] = ResolveUrl("~/images/" + file);
}
dataList.DataSource = table;
dataList.DataBind();

Troubleshooting checklist:

  • Inspect the page source / DevTools network tab to see the actual src value and any 404/403 errors.
  • Use forward slashes in URLs (/images/foo.jpg), not backslashes.
  • Keep image files in a publicly served folder (not App_Data) and check file permissions.
  • If images are stored as binary, serve them via an IHttpHandler/endpoint that returns the correct Content-Type.
  • Use ResolveUrl or VirtualPathUtility.ToAbsolute to convert ~/ application-root paths into usable URLs, or store the virtual path directly.

Reference pages:

hi shall i know whether u say the image as a binary in database or you save the image in front end and save the image path in database. If u reply for this i will help u out because i to use this type in c#. so reply me soon i will reply u tomarrow ok

cool

I am saving path of the image in the database.

problem is resolved. Actually I was storing the complete physical path of my application in the database like D:\something\myapplicating\images\img.jpg that is why images in datalist are not displayed. I changed the path to images\img.jpg and it worked. Hope this will help others

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.