<asp:GridView ID="GridView1" runat="server" EnableViewState="false" AutoGenerateColumns="false"> <columns> <asp:TemplateField HeaderText="images"> <itemtemplate> <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" /> </itemtemplate> <asp:TemplateField HeaderText="images"> <itemtemplate> <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" /> </itemtemplate> <asp:TemplateField HeaderText="images"> <itemtemplate> <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" /> </itemtemplate> <asp:TemplateField HeaderText="images"> <itemtemplate> <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" /> </itemtemplate> </columns> imgret.aspx.cs protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindFiles(); } } /// <summary> /// Binds the files. /// </summary> private void BindFiles() { DataTable table = new DataTable(); // get the connection SqlConnection _conn1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"); { // write the sql statement to execute string sql = "SELECT imageid FROM myimages Order By bid"; // instantiate the command object to fire using (SqlCommand cmd = new SqlCommand(sql, _conn1)) { // get the adapter object and attach the command object to it using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) { // fire Fill method to fetch the data and fill into DataTable ad.Fill(table); } // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection } } GridView1.DataSource = table; GridView1.DataBind(); } ShowImage.ashx <%@ WebHandler Language="C#" Class="ShowImage" %> using System; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; public class ShowImage : IHttpHandler { public void ProcessRequest (HttpContext context) { if (context.Request.QueryString["bid"] == null) return; string connStr = ConfigurationManager.ConnectionStrings["Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"].ToString(); string autoId = context.Request.QueryString["bid"]; using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand("SELECT imageid FROM myimages WHERE bid = @bid", conn)) { cmd.Parameters.Add(new SqlParameter("@bid", bid)); conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { reader.Read(); context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("myimages")]); reader.Close(); } } } } public bool IsReusable { get { return true; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)