CREATE TABLE
CREATE TABLE Tablename
(
[ImageID] [int] IDENTITY(1,1) NOT NULL,
[Images] [image] NULL,
[ImageName] [varchar](max) NULL,
)
IN THE ASPX
IN THE ASPX.CS
//These namespaces should be included
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default : System.Web.UI.Page
{
static string constr = ConfigurationManager.ConnectionStrings["giricon"].ConnectionString;
SqlConnection con = new SqlConnection(constr);//Calling connection string from the web.config
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binding();
}
}
private void binding()
{
adp = new SqlDataAdapter("Select * from tablename", con);
adp.Fill(dt);
adp.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
CREATE TABLE Tablename
(
[ImageID] [int] IDENTITY(1,1) NOT NULL,
[Images] [image] NULL,
[ImageName] [varchar](max) NULL,
)
IN THE ASPX
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" DataKeyField="ImageID" CellPadding="15" Width="500px">
<ItemTemplate>
<div>
<asp:Image ID ="img" Width ="120px" Height="120px" runat="server"
ImageUrl ='<%# "Handler.ashx?ImageID= " + Eval ("ImageID") %> '></asp:Image>
</div>
</br>
<div>
<asp:Label ID ="lblName" ForeColor="#FFFFFF" runat="server"
Text ='<%# Eval ("ImageName") %>'></asp:Label>
Text ='<%# Eval ("ImageName") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:DataList>
IN THE HANDLER PAGE
Add New Item -> Select Generic handler -> Add the below coding in the Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class Handler : IHttpHandler {
static string constr = ConfigurationManager.ConnectionStrings["addname"].ConnectionString;
SqlConnection con = new SqlConnection(constr); //Configuring using connection string from the webconfig
public void ProcessRequest (HttpContext context)
{
string imgID = context.Request.QueryString["ImageID"].ToString();
SqlCommand cmd = new SqlCommand("select * from Tablename where ImageID=" + imgID, con);
con.Open();
SqlDataReader Dr = cmd.ExecuteReader();
Dr.Read();
context.Response.BinaryWrite((byte[]) Dr["Images"]);
con.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
IN THE ASPX.CS
//These namespaces should be included
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default : System.Web.UI.Page
{
static string constr = ConfigurationManager.ConnectionStrings["giricon"].ConnectionString;
SqlConnection con = new SqlConnection(constr);//Calling connection string from the web.config
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binding();
}
}
private void binding()
{
adp = new SqlDataAdapter("Select * from tablename", con);
adp.Fill(dt);
adp.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
No comments:
Post a Comment