How to display image inside the datalist?

Posted by Prabu_Spark under ASP.NET on 11/16/2012 | Points: 10 | Views : 7241 | Status : [Member] | Replies : 3
Hi sir,
I design the website to search the employee details, but i cant able to view the image inside the datalist. Kindly give me
the solution for this problem. I attached the design and code below.


Design:
****************************************

<body>
<form id="form1" runat="server">
<div>

<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Dept id"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
<br />
<br />
<asp:DataList ID="DataList1" runat="server"
Caption="Employee Details" CellPadding="4" ForeColor="#333333"
>
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
<ItemTemplate>
<table border="2" cellpadding="2" width="200px">
<tr>
<td colspan="2" align="center">
<asp:Image ID="image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"deptid") %>' Height="90px" Width="90px" AlternateText="No Image" BorderColor="ActiveBorder" BorderStyle="Groove" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Dept ID"></asp:Label>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"deptid") %>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name "></asp:Label>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"name") %>
</td>
</tr>
</table>


</ItemTemplate>
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
</asp:DataList>
<br />

</div>
</form>
</body>

Code:
**********************

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=PRABU-PC;Initial Catalog=spark;Persist Security Info=True;User ID=prabhu;Password=spark123");
SqlCommand cmd;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
string s = "select * from dept where deptid="+Convert.ToInt32(TextBox1.Text);
cmd = new SqlCommand(s, con);
sda.SelectCommand = cmd;
sda.Fill(ds, "emp");
DataList1.DataSource = ds.Tables["emp"].DefaultView;
DataList1.DataBind();
con.Close();
}

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Mstnnhan on: 11/16/2012 [Member] Starter | Points: 25

Up
0
Down
well...!!

http://www.centplay.com/affiliate/promotion_7079/ to get more free money:

click here to get free money: http://www.centplay.com/affiliate/promotion_7079/

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

Posted by: Laurenrodriguez on: 11/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hey!
It's resolved ...

Follow this link.

http://www.aspdotnetcodes.com/Insert_Images_Database.aspx[^]


Prabu_Spark, 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
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>
</div>
<asp:DataList ID="DataList1" runat="server">
<FooterStyle BackColor="#FFCC00" ForeColor="#FF3300" />
<EditItemStyle BackColor="#FF8080" />
<AlternatingItemStyle BackColor="#FFFF99" />
<HeaderTemplate>
<center><b>Student Details</b></center>
</HeaderTemplate>
<ItemTemplate>
<table border="1">
<tr>
<th>sid</th>
<th>sname</th>
<th>saddress</th>
<th>smarks</th>
<th>pic</th>
</tr>
<tr>
<td><%#Container.DataItem("sid")%></td>
<td><%#Container.DataItem("sname")%></td>
<td><%#Container.DataItem("saddress")%></td>
<td><%#Container.DataItem("smarks")%></td>
<td><asp:Image ID="Image1" ImageUrl='<%#container.dataitem("pic")%>' runat="server" /></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</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 str As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
con.Open()
Str = "select * from student"
com = New SqlCommand(Str, con)
Dim reader As SqlDataReader
reader = com.ExecuteReader
DataList1.DataSource = reader
DataList1.DataBind()
reader.Close()
con.Close()
End If
End Sub
End Class


If this post helps you mark it as answer
Thanks

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

Login to post response