Example
---------------------
1.step 1 create table in sql(2005/2008) like below.
in sql
--------
Id = varchar(50)
Image=image
2.step2 design .aspx form like below
in .aspx
-----------
<div>
<table align="center"><tr><td>
<asp:Label ID="lblid" runat="server" Text="ID"></asp:Label></td><td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
<tr><td>Choose File</td><td><asp:FileUpload ID="FileUpload1" runat="server" />
</td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server"
Text="Submit" onclick="Button1_Click" /></td></tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"Handler2.ashx?ID="+Eval("id")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
3.step3 add one Handler.ashx to your project
4.here clearly observe in .aspx ID(ImageUrl='<%#"Handler2.ashx?ID) and in .ashx file( string id =
context.Request.QueryString["ID"];) will keep same
in Handler2.ashx
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace _3tier
{
/// <summary>
/// Summary description for Handler2
/// </summary>
public class Handler2 : IHttpHandler
{
string cs =
ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString;
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["ID"];
SqlConnection con = new SqlConnection(cs);
string query = "select * from img Where id=" + id;
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])(dr[1]));
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
5.step 5 write the code in .aspx.cs like below
.aspx.cs
------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace _3tier
{
public partial class WebForm2 : System.Web.UI.Page
{
string cs =
ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
int length = FileUpload1.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
HttpPostedFile IMG = FileUpload1.PostedFile;
IMG.InputStream.Read(imgbyte, 0, length);
SqlConnection con = new SqlConnection(cs);
string query1 = "insert into img(id,image) values(@id,@image)";
SqlCommand cmd = new SqlCommand(query1, con);
con.Open();
cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = TextBox1.Text;
//cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = TextBox2.Text;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
cmd.ExecuteNonQuery();
string query2 = "select * from img";
SqlDataAdapter da = new SqlDataAdapter(query2, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
TextBox1.Text = string.Empty;
}
}
}