display image in gridview from DB where database has image URL not image [Resolved]

Posted by Rickeybglr under ASP.NET on 8/6/2012 | Points: 10 | Views : 13959 | Status : [Member] | Replies : 5
Hi,

i want to display photo with some info store in DB. DB has only image URL not the image. images are stored in asp.net folder.
suggest guys...
i got idea..tht we can map image column to image URL. to retrieve image url with data i have written the SP.




Responses

Posted by: Ranjeet_8 on: 8/6/2012 [Member] [MVP] Gold | Points: 50

Up
0
Down

Resolved
       

<asp:TemplateField HeaderText="Image">
<ItemTemplate >
<asp:Image ID="Image1" runat="server" ImageUrl ='<%#eval("ImageURL_ColumnName") %>' height="120px" Width="150px" />
</ItemTemplate>
</asp:TemplateField>


Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 8/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi..
Refer the below link to view the image using URL form db
http://www.dotnetcode.in/2011/06/how-to-store-imageurl-in-database-and.html

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 8/6/2012 [Member] Starter | Points: 25

Up
0
Down
here first you need to set the path of your folder in which folder you have stored image. Than imagename stores the name of your photo from database than you simply can retrive the photo from database using imagename.
string path = Server.MapPath(("~/Admin/Images/" + fileuploadImagepath.FileName).ToString());
string imagename = Path.GetFileName(fileuploadImagepath.PostedFile.FileName);

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 8/6/2012 [Member] Starter | Points: 25

Up
0
Down
Thnks everyone ..

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
First create a table student
Create table student (sid varchar(50),sname varchar(50),saddress varchar(50), smarks int,pic nvarchar (50))

Store the picture in the program folder and save there name in pic column of the table with there extension like (.gif,.jpg,.bmp).

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="1" BackColor="#FF9999"
onpageindexchanging="GridView1_PageIndexChanging">
<HeaderStyle BackColor="#FFCC99" />
<Columns>
<asp:TemplateField HeaderText="Image" SortExpression="pic">
<ItemTemplate>
<asp:Image ID="Image1" Height="60" Width="60" ImageUrl='<%# Bind("pic") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" SortExpression="sid">

<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("sid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Name" SortExpression="sname">

<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("sname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="Address" SortExpression="saddress">

<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("saddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Marks" SortExpression="smarks">

<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("smarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Default.aspx.vb code

Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Dim str As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bindgrid()
End Sub

Sub bindgrid()
con.Open()
str = "SELECT * FROM student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet()
sqlda.Fill(ds, "student")
GridView1.DataSource = ds
GridView1.DataMember = "student"
GridView1.DataBind()
con.Close()
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
bindgrid()
End Sub
End Class


If this post helps you mark it as answer
Thanks

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response