9
Answers

how to display image in datalist aspnet c#

Photo of Rajveer Singh

Rajveer Singh

9y
356
1
Pet.aspx
 
<p>
<asp:DataList ID="DataList6" runat="server" CellPadding="4" ForeColor="#333333" RepeatColumns="5" RepeatDirection="Horizontal">
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"/>
<HeaderTemplate>Sheep Details</HeaderTemplate>
<ItemStyle BackColor="#E3EAEB" />
<ItemTemplate>
<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("Picture", "petpic/") %>' style="padding-left:40px"/><br />
<br />
Breed:
<asp:Label ID="BreedLabel" runat="server" Text='<%# Eval("Breed") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
Age:
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
<br />
Gender:
<asp:Label ID="GenderLabel" runat="server" Text='<%# Eval("Gender") %>' />
<br />
Weight:
<asp:Label ID="WeightLabel" runat="server" Text='<%# Eval("Weight") %>' />
<br />
Date:
<asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</p>
 
 
Pet.aspx.cs
 
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
if (!IsPostBack)
{
BindData5();
}
}
 
 
protected void BindData5()
{
DataSet ds = new DataSet();
string cmdstr = "Select top 6 * from Sheep";
SqlCommand cmd = new SqlCommand(cmdstr, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DataList6.DataSource = ds.Tables[0];
DataList6.DataBind();
}
 
Above is the codes.. so i have a folder in my project called "petpic"
when i insert picture to database it goes into that folder..
but it doesnt show on the datalist.. what is wrong.. help me 
 

Answers (9)

0
Photo of Vipan Sharma
NA 1.1k 935 9y
<asp:Image Width="100" ID="imgEmp" ImageUrl='<%# Bind("Picture", "~/{0}") %>' runat="server" />
 
 
I have updated the code. Check it will work for you. let me know if still facing same issue.
Accepted
1
Photo of Vipan Sharma
NA 1.1k 935 9y
Hello,
 
Can you please tell me  ? What you shaving in database . image name or image path with Name .
 
<asp:Image Width="100" ID="imgEmp" ImageUrl='<%# Bind("Picture", "~/petpic/{0}") %>' runat="server" />
 
Let me know if any help required. 
Try This Hope it will help you . Accept answer , SO it will help next developer to solve same issue
 
 
0
Photo of Vipan Sharma
NA 1.1k 935 9y
 
Great. Accept answer, if it works for you . It will help next develop for solve same issue
 
0
Photo of Rajveer Singh
NA 74 4.4k 9y
ok thank u it worked
0
Photo of Vipan Sharma
NA 1.1k 935 9y
 
Accept answer if it works for you .
 
 
 
0
Photo of Rajveer Singh
NA 74 4.4k 9y
and in coding: if (PictureUpload.HasFile == true) { petImage = Path.GetFileName(PictureUpload.PostedFile.FileName); PictureUpload.SaveAs(Server.MapPath("petpic/" + petImage)); }
0
Photo of Rajveer Singh
NA 74 4.4k 9y
in the db table: Picture varchar(max) once i add picture it shows: petpic/sheep.jpg
0
Photo of Rajveer Singh
NA 74 4.4k 9y
hello this is the link i refer to do the above.. but the image doesnt appear
0
Photo of Rajeesh Menoth
NA 24.7k 629.6k 9y
Hi,
 
Read the below article..
 
 
http://www.dotnetfox.com/articles/how-to-display-images-in-datalist-control-using-Asp-Net-with-C-Sharp-1037.aspx