Reputation: 7777
I have this ASP.NET code:
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None">
<Columns>
<asp:ImageField DataImageUrlField="image" >
</asp:ImageField>
<asp:BoundField DataField="imagename" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT * from [Image]", connection);
SqlDataAdapter ada = new SqlDataAdapter(command);
ada.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
Here in datatable, I have imagename and image as columns containing values
blue F:\R&D\RD\RD\Images\nextlabel.gif
red F:\R&D\RD\RD\Images\fDSC03578.JPG
green F:\R&D\RD\RD\Images\fDSC03556.JPG
Under this directory I have the images too. I get the image name in the grid but the image is not getting displayed. This is an easy thing to do, but still I am not able to achieve it. Where am I going wrong?
Upvotes: 1
Views: 4306
Reputation: 649
If you have copied the image to your project's Image directory from some other path, just remove the image from the original path and keep the copy only in the Image directory. I have seen it pointing to the old path sometimes and that creates problem.
Upvotes: 0
Reputation: 8421
Your trying to access the local path F:\R&D\RD\RD\Images\nextlabel.gif
from your web server, you can't do this in a situation where the client doesn't have access to the web server. You can map the directory F:\R&D\RD\RD\Images\
to a virtual directory located on the web server. In this way you can create a virtual directory under your root such as mysite.com/images
that refers to the location F:\R&D\RD\RD\Images\
.
Once the virtual directory has been created you can then refer to the images within that directory as if they were relative to the web server. To make this work you would also have to be able to modify the image in the database to store just the file name, or do some processing on the path to grab the file name.
<asp:ImageField DataImageUrlField="image" DataImageUrlFormatString="~/images/{0}">
</asp:ImageField>
Here's an article on How to: Create and Configure Virtual Directories in IIS 5.0 and 6.0 or if your using IIS 7.0. Take care to properly configure security and authentication for the virtual directory.
An alternate way to handle this would be by creating a custom HTTP handler to serve images from the file system or database. Here's an example of using an HTTP Handler to load an image from a database and here's one that shows how to pull images from the file system.
Upvotes: 3
Reputation: 1924
it is not wise to hold absolute path of images in your table..because it is bound to your current file system...in your image url fields you should have just name of a image... so...
copy your images from your local disk to your server dir, for example your_app/images/ change your images url's in your table to names of images, for example:
nextlabel.gif
change your markup of gridview to be:
<asp:imagefield dataimageurlfield="image"
dataimageurlformatstring = "~/images/{0}"/>
hope this helps
Upvotes: 0
Reputation: 337
I'm pretty sure that to use an image field like that the images would need to be "on the web" so that the users browser can load it. If the images are not in a web accessible directory then you'll need to use a .aspx file that has a content type of image and loads the images into itself (fairly expensive for busy sites).
Upvotes: 0