Thursday, 11 May 2017

How to upload image in database and show on your web page

Create Default.aspx web Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 63%;
        }
        .auto-style2 {
            width: 346px;
        }
        .auto-style3 {
            width: 146px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="auto-style1" style="background-color: #339933; margin-left: 143px;">
            <tr>
                <td colspan="3" align="center" style="color: #990000">ShowImages</td>
            </tr>
            <tr>
                <td class="auto-style3">ImageID</td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtImgId" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style3">ImageName</td>
                <td class="auto-style2">
                    <asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style3">ImageUpload</td>
                <td class="auto-style2">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td>
                    <asp:Label ID="lblMsg" runat="server" ForeColor="Red" Text="Message"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="center" class="auto-style3">
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </td>
                <td align="center" class="auto-style2">
                    <asp:Button ID="btnShow" runat="server" Text="Show" OnClick="btnShow_Click" />
                </td>
                <td align="center">
                    <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:Image ID="Image1" runat="server" /><br />
                    <asp:Label ID="lblDate" runat="server" Text="Date"></asp:Label><br />
                    <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Create Default.aspx.cs file
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;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string StrCon = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection SqlConn = new SqlConnection(StrCon);
            string fileimages = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("~/images/" + fileimages));
            string SqlQuery = "Insert into ImagesTable(Images,ImageId,ImageName) values('" + fileimages + "','" + txtImgId.Text + "','" + TextBoxName.Text + "')";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlConn);
            SqlConn.Open();
            int RecordsAffected;
            RecordsAffected = SqlComm.ExecuteNonQuery();
            if (RecordsAffected > 0)
            {
                Session["ImageId"] = txtImgId.Text;
                //Response.Redirect("~/Default2.aspx");
            }
            else
            {
                lblMsg.Text = "No Records Inserted";
            }
            SqlConn.Close();
        }
        protected void btnShow_Click(object sender, EventArgs e)
        {
            string StrCon = ConfigurationManager.AppSettings["ConnectionString"];
            string imagepath = "";
            SqlConnection SqlConn = new SqlConnection(StrCon);
            SqlConn.Open();
            string Imageid = (string)Session["ImageId"];
            string SqlQuery = "Select Images,Date,ImageName from ImagesTable where ImageId='" + Imageid + "'";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlConn);
            //SqlConn.Open();
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader(CommandBehavior.CloseConnection);
            if (dr.HasRows)
            {
                dr.Read();
                imagepath = dr["Images"].ToString();
                Image1.ImageUrl = "~/Images/" + imagepath;
                lblDate.Text = "Date :" + dr["Date"].ToString();
                lblName.Text = "Name :" + dr["ImageName"].ToString();
            }
            SqlConn.Close();
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            txtImgId.Text = "";
            TextBoxName.Text = "";
        }
    }
}

web.config file
<configuration>
<appSettings>
    <add key="ConnectionString" value="Server=PRADEEP-HP;Database=Decofloore2;User Id=User_006;password=user006"/>
  </appSettings>
</configuration>
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment