.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Resource Bookmark and Share   

Display image in GridView

Posted By:satyapriyanayak       Posted Date: March 25, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

Display image in GridView

First create a table student

Create table student (sid varchar(50),sname varchar(50),saddress varchar(50), smarks int,pic nvarchar (50))


Store the picture in the program folder and save there name in pic column of the table with there extension like (.gif,.jpg,.bmp).


Default.aspx code


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>



    <form id="form1" runat="server">


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            AllowPaging="True" PageSize="1" BackColor="#FF9999"


            <HeaderStyle BackColor="#FFCC99" />


                <asp:TemplateField HeaderText="Image" SortExpression="pic">                


                        <asp:Image ID="Image1" Height="60" Width="60" ImageUrl='<%# Bind("pic") %>' runat="server" />                       



                <asp:TemplateField HeaderText="Id" SortExpression="sid">



                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("sid") %>'></asp:Label>




                <asp:TemplateField HeaderText="Name" SortExpression="sname">



                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("sname") %>'></asp:Label>





                <asp:TemplateField HeaderText="Address" SortExpression="saddress">



                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("saddress") %>'></asp:Label>




                <asp:TemplateField HeaderText="Marks" SortExpression="smarks">



                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("smarks") %>'></asp:Label>











Default.aspx.vb code


Imports System.Data.SqlClient

Imports System.Data

Partial Class _Default

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim con As New SqlConnection(strConnString)

    Dim com As SqlCommand

    Dim sqlda As SqlDataAdapter

    Dim ds As DataSet

    Dim str As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub


    Sub bindgrid()


        str = "SELECT * FROM student"

        com = New SqlCommand(str, con)

        sqlda = New SqlDataAdapter(com)

        ds = New DataSet()

        sqlda.Fill(ds, "student")

        GridView1.DataSource = ds

        GridView1.DataMember = "student"



    End Sub


    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        GridView1.PageIndex = e.NewPageIndex


    End Sub

End Class