How to save image in data base and retrieve it on datalist in asp.net c#

Data base SQL Server



Script for creating table 

USE [uploaddata]
GO

/****** Object:  Table [dbo].[tab]    Script Date: 04/05/2014 17:32:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tab](
          [Id] [int] IDENTITY(1,1) NOT NULL,
          [ImageName] [nvarchar](max) NULL,
 CONSTRAINT [PK_tab] PRIMARY KEY CLUSTERED
(
          [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO




Source Code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            Image&nbsp;&nbsp;&nbsp;
            <asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
            <br />
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />
        </div>
        <div>
            <asp:DataList ID="dlImages" runat="server" RepeatColumns="4" CellPadding="4">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" ImageUrl='<%# Bind("ImageName", "~/images/{0}") %>'
                        Width="182" Height="284" runat="server" />
                    <br />
                </ItemTemplate>
                <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
            </asp:DataList>
        </div>
    </div>
    </form>
</body>
</html>



Code behind (c#) :-


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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=NEERAJ-PC; uid=sa; Pwd=12345678;database=uploaddata");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataList();
        }
    }
    protected void BindDataList()
    {
        con.Open();
        SqlCommand command = new SqlCommand("SELECT ImageName from tab", con);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dlImages.DataSource = dt;
        dlImages.DataBind();
        con.Close();
    }
    protected void btnupload_Click(object sender, EventArgs e)
   {
     try
      {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("images/" + filename));
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into tab(ImageName) values(@ImageName)", con);
            cmd.Parameters.AddWithValue("@ImageName", filename);
            cmd.ExecuteNonQuery();
            con.Close();
            BindDataList();
      }
     catch (Exception ex)
     {
         Response.Write("<script>alert('Please select the image !!')</script>");
     }

    }
}


Note: - first you can create a images name folder in Solution Explorer


Out-Put:-



How to save image in data base and retrieve it on datalist in asp.net c# How to save image in data base and retrieve it on datalist in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 6:12:00 PM Rating: 5

No comments:

Powered by Blogger.