Monday, July 23, 2018

How to store image or file save in database in binary datatype / image data type in sql server | Sradha WebCreations


How to store image or file save in database in binary datatype / image data type in sql server

how to store image in binary format in  MS Sql Server Database 


 http://sradhawebcreations.com/


HttpHandler.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpHandler.aspx.cs" Inherits="HttpHandler" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>

       Enter ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       Enter Name&nbsp;&nbsp; <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        Select image&nbsp;  <asp:FileUpload ID="FileUpload1" runat="server" /><br />
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" /><br />

       <asp:Image ID="Image1"  runat="server" Height="137px" Width="130px" /><br />
       
        <asp:DropDownList ID="TextBox3" runat="server">
        </asp:DropDownList>
       
        <asp:Button ID="Button2" runat="server" Text="Search" onclick="Button2_Click" />

    </div>
    </form>
</body>
</html>

 http://sradhawebcreations.com/
HttpHandler.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class HttpHandler : System.Web.UI.Page
{
    //static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\websites\HttpHandlerDemo\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            filldropdown();
        }
    }

    public void filldropdown()
    {
        SqlCommand cmd = new SqlCommand("Select EmpID from Tbl_Emp", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        TextBox3.Items.Clear();
        if (dr.HasRows)
        {
            //TextBox3.DataSource = dr["EmpID"].ToString();
            //TextBox3.DataBind();

            while (dr.Read())
            {
                TextBox3.Items.Add(dr["EmpID"].ToString());
            }
        }
        con.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("insert into Tbl_Emp values(@id,@name,@image)",con);
        cmd.Parameters.AddWithValue("@id", TextBox1.Text);
        cmd.Parameters.AddWithValue("@name", TextBox2.Text);

        int img = FileUpload1.PostedFile.ContentLength;

        byte[] msdata = new byte[img];

        FileUpload1.PostedFile.InputStream.Read(msdata,0,img);

        cmd.Parameters.AddWithValue("@image", msdata);

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd.ExecuteNonQuery();

        con.Close();

        filldropdown();

        Response.Write("Data Saved ....");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from Tbl_Emp where EmpID=@id", con);
        cmd.Parameters.AddWithValue("@id", TextBox3.Text);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows && dr.Read())
        {
            TextBox1.Text = dr["EmpID"].ToString();
            TextBox2.Text = dr["EmpName"].ToString();
            Image1.ImageUrl = "Handler.ashx?EmpID=" + TextBox3.Text;
        }
        else
        {
            Response.Write("Record With This ID Note Found");
        }
       con.Close();

    }
}
Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;

public class Handler : IHttpHandler {

  //  static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\websites\HttpHandlerDemo\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    public void ProcessRequest (HttpContext context) {
       // context.Response.ContentType = "text/plain";
       // context.Response.Write("Hello World");
        SqlCommand cmd = new SqlCommand("select EmpPic from Tbl_Emp where EmpID=@EmpID",con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd.Parameters.AddWithValue("@EmpID", context.Request.QueryString["EmpID"].ToString());
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows && dr.Read())
        {
            context.Response.BinaryWrite((byte[])(dr["EmpPic"]));
        }
      
        con.Close();
       
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}


 http://sradhawebcreations.com/


No comments:

Post a Comment