Sreekanth Reddy

Sreekanth Reddy

  • 352
  • 4.8k
  • 420.6k

Binding image into gridview in asp.net

Oct 31 2013 3:57 AM
Hai friends,
I am alearner of a.s.p.net.
I need to bind student image of type image into my gridview from database.
database contains:
stuid(int)
stuname(varchar)
stuimage(image)



this is my aspx code:


<div>
<asp:GridView ID="gvImages" CssClass="Gridview" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<Columns>
<asp:BoundField HeaderText = "student id" DataField="stuid" />
    <asp:BoundField HeaderText="student name" DataField="stuname" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"Handler.ashx?id="+ Eval("stuimage") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>

<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
    </div>




& This is my aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("server=.;database=test;trusted_connection=true");
            SqlCommand command = new SqlCommand("SELECT * from student", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            gvImages.DataSource = dt;
            gvImages.DataBind();
            gvImages.Attributes.Add("bordercolor", "black");
        }
    }
}


Please help me friends....

Answers (2)