Upload, Save and Retreive image from Database In ASP.NET

FileUpload.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="WebApplication8.FileUpload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fu" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Label ID="imgName" runat="server" Text=""></asp:Label>
        <asp:Image ID="img" runat="server" Width="50" Height="50" />
    </div>
    </form>
</body>
</html>



FileUpload.aspx.cs

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;
using System.Configuration;
namespace WebApplication8
{
    public partial class FileUpload : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con1 = new SqlConnection(cs);
            SqlCommand scmd = new SqlCommand();

            scmd.Connection = con1;
            scmd.CommandText = "select * from myImages";

            con1.Open();

            SqlDataReader rd = scmd.ExecuteReader();

            while(rd.Read())
            {
                imgName.Text = rd[1].ToString();
                img.ImageUrl = rd[2].ToString();
            }

            con1.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if(fu.HasFile)
            {
                if(fu.PostedFile.ContentType=="image/jpeg")
                {
                        if(fu.PostedFile.ContentLength<100240)
                    {
                        string filename = Path.GetFileName(fu.FileName);
                        string path= "~/images/" +filename;
                        fu.SaveAs(Server.MapPath(path));
                        SqlConnection con = new SqlConnection(cs);
                        SqlCommand cmd = new SqlCommand();

                        cmd.Connection = con;
                        cmd.CommandText = "Insert into myImages values(@name,@path)";

                        con.Open();

                        cmd.Parameters.AddWithValue("@name", filename);
                        cmd.Parameters.AddWithValue("@path",path);

                        if(cmd.ExecuteNonQuery()>0)
                        {
                            Label1.Text = "File Uploaded and Data inserted in Database";
                        }

                   
                    }
                    else
                    {
                        Label1.Text = "File Size Should be Less Than 100 kb";
                    }
                }
                else
                {
                    Label1.Text = "Only Jpeg Files are allowed";
                }
               
            }
        }
    }
}

Comments

Popular posts from this blog

How to Delete Data from Database Using ASP.NET

Login Form with Database in ASP.NET