Share Blog

Monday, May 12, 2014

How to Image Upload in Database And Show using Asp.net

How to Upload Image in Folder and Database using Asp.net



------Create Table -----------
\


------------------------------Insert Image------------------------------------------------------------------
--------Create Images Folder in Solution Explore-----

---------------------Image For imageexample.aspx 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.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    SqlDataReader dr;
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=adonetpractices;Integrated Security=True");
    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 SaveButton1_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into employeeimage values(@id,@name,@img)",con);
        cmd.Parameters.AddWithValue("@id",Convert.ToInt32(TextBox1.Text));
        cmd.Parameters.AddWithValue("@name", TextBox2.Text);
        cmd.Parameters.AddWithValue("@img",FileUpload1.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
        cmd.ExecuteNonQuery();
        Response.Write("<script>alert('Data Save Successfully')</script>");
        con.Close();
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
    //this  code  for  retrive image  from Database  and  Image Folder which are exist  in Website...............
    protected void SelectButton2_Click(object sender, EventArgs e)
    {
        con.Open();
       SqlCommand  cmd = new SqlCommand("select * from employeeimage where id=@id", con);
        cmd.Parameters.AddWithValue("@id", Convert.ToInt32(TextBox1.Text));
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            //TextBox2.Text = dr["Name"].ToString();
            Image2.ImageUrl = "~/Images/" + dr["Img"].ToString();

        }

        con.Close();
    }
    //this  code  for  delete image  from  Database  and  Image Folder which are exist  in  Website...............
    protected void DeleteButton1_Click(object sender, EventArgs e)
    {
        DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/images/"));
        foreach (FileInfo ff in dd.GetFiles())
        {
            if(ff.FullName == Path.GetFileName("~/image/"))// if donot deleted from image folder but delete from database
           
            {

                ff.Delete();
            }
        }
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from employeeimage where id=@id", con);
        cmd.Parameters.AddWithValue("@id",Convert.ToInt32(TextBox1.Text));
        cmd.ExecuteNonQuery();
        con.Close();
        TextBox1.Text = "";
        Label1.Text = "Delete Successfully Image " + "For id" + "-" + TextBox1.Text + "";
       
    }

    //this  code  for  Update  image  in Database  and  Image Folder and  old  image  deleted  from Image Folder
    protected void UpdateButton1_Click(object sender, EventArgs e)
    {
       
            DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/images/"));
            foreach (FileInfo ff in dd.GetFiles())
            {
                if (ff.FullName == Path.GetFileName(Image2.ImageUrl))
                    ff.Delete();
            }
            con.Open();
            SqlCommand cmd = new SqlCommand("update  employeeimage set name=@name,img=@img where id=@id", con);
            cmd.Parameters.AddWithValue("@id", Convert.ToInt32(TextBox1.Text));
            cmd.Parameters.AddWithValue("@name", TextBox2.Text);
            if (FileUpload1.HasFile)
            {
                cmd.Parameters.AddWithValue("@img", FileUpload1.FileName);
            }
            else
            {
                cmd.Parameters.AddWithValue("@img", Image2.ImageUrl);
            }
            FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Data update Successfully')</script>");
        }
    


No comments:

Post a Comment