Hi Payalsh,
Please refer below sample.
DataBase
I have made use of the following table tblFilePath with the schema as follows.
I have inserted few records in the table.
HTML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" CellPadding="4" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="120px">
<tr>
<td align="center">
<asp:Image ID="imgFilePath" ImageUrl='<%# Eval("Path") %>' runat="server" Height="100"
Width="100" />
</td>
</tr>
<tr>
<td align="center">
<%# Eval("ImageName") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Namespace
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDataList();
}
}
private void BindDataList()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblFilePath", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.DataList1.DataSource = cmd.ExecuteReader();
this.DataList1.DataBind();
con.Close();
}
}
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image imgFile = e.Item.FindControl("imgFilePath") as Image;
imgFile.ImageUrl = imgFile.ImageUrl.Split(',')[0];
}
}
VB.Net
Private Sub BindDataList()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tblFilePath", con)
cmd.CommandType = CommandType.Text
con.Open()
Me.DataList1.DataSource = cmd.ExecuteReader()
Me.DataList1.DataBind()
con.Close()
End Using
End Using
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim imgFile As Image = TryCast(e.Item.FindControl("imgFilePath"), Image)
imgFile.ImageUrl = imgFile.ImageUrl.Split(","c)(0)
End If
End Sub
End Class
Screenshort