Tuesday, 21 May 2013

How to Use FileUpload in Asp.Net ,How to Save Image ,Picture in Website Folder


How to Use FileUpload in Asp.Net ,How to Save Image ,Picture in Website Folder


Create Table: 
Table Name: UserImage
.ASPX CODE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload_Database.aspx.cs" Inherits="FileUpload_Database" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div><p>How to Use FileUpload in Asp.Net ,How to Save Image ,Picture in Website Folder
</p></div>
    <div>
    <table >
   
        <tr>
            <td colspan="3"><center><asp:Label ID="Label1" runat="server" Text="Save Image in Image folder and Database" Font-Bold="true"></asp:Label></center>
              
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="SrNo" runat="server" Text="SrNo"></asp:Label>
            </td>
            <td class="style2">
                <asp:TextBox ID="txtsrno" runat="server"></asp:TextBox>
            </td>
            <td rowspan="5">
                <asp:Image ID="Image1" runat="server" Height="91px" Width="99px" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Name"></asp:Label>
            </td>
            <td class="style2">
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="Image"></asp:Label>
            </td>
            <td class="style2">
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">

                <asp:Button ID="BSave" runat="server" onclick="BSave_Click" Text="Save" />
            <asp:Button ID="BSelect" runat="server" onclick="BSelect_Click" Text="Select" />
                <asp:Button ID="BDelete" runat="server" onclick="BDelete_Click" Text="Delete" />
&nbsp;<asp:Button ID="BUpdate" runat="server" onclick="BUpdate_Click" Text="Update" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                <asp:Label ID="Message" runat="server" Text="Label" Visible="False"></asp:Label>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

.CS CODE

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class FileUpload_Database : System.Web.UI.Page
{
    string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
   
    SqlCommand cmd;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //this  code  for  Saving image  in Database  and  Image Folder which are exist  in      Website...............
    protected void BSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strcon);

        con.Open();
        cmd = new SqlCommand("insert into UserImage (SrNo,Name,UserImg) values (@SrNo,@Name,@UserImg)", con);
        cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
        cmd.Parameters.AddWithValue("@Name", txtname.Text);
        cmd.Parameters.AddWithValue("@UserImg", FileUpload1.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
        cmd.ExecuteNonQuery();
        Message.Visible = true;
        Message.Text = "Add Successfully ";
        con.Close();
        txtname.Text = "";

    }
    protected void BSelect_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strcon);
        con.Open();
        cmd = new SqlCommand("select * from UserImage where SrNo=@SrNo", con);
        cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            txtname.Text = dr["Name"].ToString();
            Image1.ImageUrl = "~/Image/" + dr["UserImg"].ToString();

        }

        con.Close();
  
    }
    protected void BDelete_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strcon);
        DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/~/Image"));
        foreach (FileInfo ff in dd.GetFiles())
        {
            if (ff.Name == Path.GetFileName(Image1.ImageUrl))
                ff.Delete();
        }

        con.Open();
        cmd = new SqlCommand("delete from UserImage where SrNo=@SrNo", con);
        cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
        cmd.ExecuteNonQuery();
        con.Close();
        txtname.Text = "";
        Message.Visible = true;
        Message.Text = "Delete Successfully Image " + "For SrNo" + "-" + txtsrno.Text + "";
    }
    protected void BUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(strcon);

            DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/~/Image"));
            foreach (FileInfo ff in dd.GetFiles())
            {
                if (ff.Name == Path.GetFileName(Image1.ImageUrl))
                    ff.Delete();
            }

            con.Open();
            cmd = new SqlCommand("update  UserImage set Name=@Name, UserImg=@UserImg  where SrNo=@SrNo", con);
            cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            if (FileUpload1.HasFile)
            {
                cmd.Parameters.AddWithValue("@UserImg", FileUpload1.FileName);
            }
            else
            {
                cmd.Parameters.AddWithValue("@UserImg", Image1.ImageUrl);
            }

            FileUpload1.SaveAs(Server.MapPath("`/~/Image/") + FileUpload1.FileName);
            cmd.ExecuteNonQuery();
            con.Close();
            txtname.Text = "";
            Message.Visible = true;
            Message.Text = "update Successfully Image " + "For SrNo" + "-" + txtsrno.Text + "";
        }
        catch (Exception ex)
        {
            Message.Visible = true;
            Message.Text = ex.Message;
        }
      
    }
}


No comments:

Post a Comment