Deepak Ratan

Deepak Ratan

  • NA
  • 175
  • 50.3k

how to display image from sql table in gridview?

May 19 2015 8:32 AM
hi i tried to display image from sql table to gridview but its not displaying this s my code to bind gridview with sql table records..
 
 sql.aspx.cs
 
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
         lblsession.Text = "Welcome" + Convert.ToString(Session["UName"]);
         sessionimage();
   }
}
private void sessionimage()
{
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
      try
     {
         string UName = (string)Session["UName"];
         SqlDataAdapter Adp = new SqlDataAdapter("select Photo from TBL_PBLogin where UName='"+ UName +"'", con);
         DataTable Dt = new DataTable();
         con.Open();
         Adp.Fill(Dt);
         gridviewphoto.DataSource = Dt;
         gridviewphoto.DataBind();
         con.Close();
      }
catch (Exception ex)
   {
      throw new Exception(ex.ToString());
   }
}
 
sql.aspx 
 
<div>
      <asp:Label ID="lblsession" runat="server" ForeColor="white" CssClass="label"></asp:Label>
      
       <asp:GridView ID="gridviewphoto" runat="server" AutoGenerateColumns="false" BackColor="#CC3300" ForeColor="Black" ShowHeader="false"          GridLines="None">
      <Columns>
               <asp:TemplateField>
                     <ItemTemplate>
                              <img alt="" src='<%#Eval("Photo")%>' height="50px" width="50px" />
                     </ItemTemplate>
               </asp:TemplateField>
      </Columns>
      </asp:GridView>
</div>
 

Answers (45)