how to upload image save ,retrieve and delete it in asp.net c#


Database (SQL Server)




Script For create the above table:-


USE [uploaddata]
GO

/****** Object:  Table [dbo].[tab]    Script Date: 04/05/2014 21:56:54 ******/
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,
          [UserImagename] [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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <table class="style1">
                <tr>
                    <td align="right">
                        Image
                        Upload </td>
                    <td>
            <asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        Image Name</td>
                    <td>
                        <asp:TextBox ID="txtimg" runat="server" Width="180px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                       
                        </td>
                    <td>
                        <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btndel" runat="server" Text="Delete Image"
                            OnClick="btndel_Click" /></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
            <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 />
                    <asp:Label ID="categoryLabel" runat="server"   Text='<%# Eval("UserImagename") %>'/>
                    </ItemTemplate>
                     <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
            </asp:DataList>
                        </td>
                </tr>
            </table>     
       </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,UserImagename 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,UserImagename) values(@ImageName,@UserImagename)", con);
            cmd.Parameters.AddWithValue("@ImageName", filename);
            cmd.Parameters.AddWithValue("@UserImagename", txtimg.Text);
            cmd.ExecuteNonQuery();
            con.Close();

            txtimg.Text = string.Empty;
            BindDataList();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Please select the image !!')</script>");
        }

    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cm = new SqlCommand("delete from tab where UserImagename =('" + txtimg.Text + "')", con);
        cm.ExecuteNonQuery();
        con.Close();
        txtimg.Text = string.Empty;
        BindDataList();
    }
}

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

Out-Put:-


how to upload image save ,retrieve and delete it in asp.net c# how to upload image save ,retrieve and delete it in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 11:55:00 PM Rating: 5

No comments:

Powered by Blogger.