16 August 2013

Display Image in GridView Using ASP.NET & sqlserver.


sql server query:
 
CREATE TABLE Images(Id int identity(100,1) primary key,Image_name varchar(200),Image nvarchar(max))

asp.net code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <h3 style="color: #0000FF; font-style: italic">Display Image in GridView Using ASP.NET</h3>
    <div>
    <asp:FileUpload ID="fileupload" runat="server" />
        <br />
        <asp:Button ID="upload" runat="server" Font-Bold="true" Text="Upload" OnClick="upload_Click" />
        <br />
        <br />
    </div>
        <div>
            <asp:GridView runat="server" ID="gdImage" HeaderStyle-BackColor="Tomato"  AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="ImageId" HeaderText="ImageId" />
                    <asp:BoundField DataField="ImageName" HeaderText="ImageName" />
                    <asp:ImageField DataImageUrlField="Image" HeaderText="Image"></asp:ImageField>                   
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

c#.net 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;
using System.IO;
using System.Configuration;
public partial class Display_Image : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void upload_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = Path.GetFileName(fileupload.PostedFile.FileName);
            fileupload.SaveAs(Server.MapPath("~/Images/" + filename));
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["ImageSql"].ConnectionString);
            con.Open();
            cmd = new SqlCommand("insert into Image_Details (ImageName,Image) values(@ImageName,@Image)", con);
            cmd.Parameters.AddWithValue("@ImageName", filename);
            cmd.Parameters.AddWithValue("@Image", "Images/" + filename);
            cmd.ExecuteNonQuery();
            da = new SqlDataAdapter("select * from Image_Details",con);
            ds = new DataSet();
            da.Fill(ds);
            gdImage.DataSource = ds;
            gdImage.DataBind();
        }
        catch (Exception ex)
        {
            upload.Text = ex.Message;
        }
    }
}

No comments:

Post a Comment