Insert image using handler


this code contains a syntax for inserting image to tne database and using handler for retriving the image

Table Design



PersonName varchar
PersonEmail varchar
sex varchar
personimage image
personImageType varchar


Namespace details



do check out for the following namespace details


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Windows.Forms


Connection Strings



The connection string is as follows


Dim connection As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString


Inserting image



Drag and drop a fileupload control and a button in the button click event do the following thing


Public partial Class Default2
Inherits System.Web.UI.Page
Dim connection As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Dim ds As DataSet = New DataSet()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
If FileUpload1.PostedFile <> Nothing And FileUpload1.PostedFile.FileName <> "" Then
Dim myimage() As Byte = New Byte(FileUpload1.PostedFile.ContentLength) {}
Dim Image As HttpPostedFile = FileUpload1.PostedFile
Image.InputStream.Read(myimage, 0, CType(FileUpload1.PostedFile.ContentLength, Integer))
Dim con As SqlConnection = New SqlConnection(connection)
Dim storepersonDetail As SqlCommand = New SqlCommand("Insert into Person " + "(PersonEmail, Sex, DOB,PersonImage,personImageType) " + " values (@PersonEmail,@Sex,@DOB,@PersonImage,@PersonImageType)",con)
storepersonDetail.Parameters.Add("@PersonEmail", SqlDbType.VarChar, 255).Value = TextBox3.Text
storepersonDetail.Parameters.Add("@Sex", SqlDbType.VarChar, 10).Value = TextBox2.Text
storepersonDetail.Parameters.Add("@DOB", SqlDbType.DateTime).Value = Convert.ToDateTime(TextBox4.Text)
storepersonDetail.Parameters.Add("@PersonImage", SqlDbType.Image, myimage.Length).Value = myimage
storepersonDetail.Parameters.Add("@PersonImageType", SqlDbType.VarChar, 255).Value = FileUpload1.PostedFile.ContentType
con.Open()
storepersonDetail.ExecuteNonQuery()
con.Close()

GridView2.DataSource = FetchAllImagesInfo()
GridView2.DataBind()
GridView2.Visible = True
End If
End Sub


To get the information on images



This method will return all the records with their image data from the table and then you have to load the images into the GridView control


Public Function FetchAllImagesInfo() As DataTable
Dim con As SqlConnection = New SqlConnection(connection)
Dim sql As String = "Select * from person"
Dim da As SqlDataAdapter = New SqlDataAdapter(sql,con)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Function


Use of handler



Handler allows you to process a request and return a response to the browser.t can handle only one request at a time, which in turn gives high performance

Create a handler to perform image retrieval This method will return binary data to the incoming request. In this method, we do normal data retrieval process and return only the Image_Content field as bytes of array.


Public Sub ProcessRequest(ByVal context As HttpContext)
Dim con As SqlConnection = New SqlConnection(Connection)
con.Open()
Dim sql As String = "Select PersonImage, PersonImageType from Person where PersonID=@PersonID"
Dim cmd As SqlCommand = New SqlCommand(sql,con)
Dim PersonID As SqlParameter = New SqlParameter("@PersonID",SqlDbType.Int,0)
PersonID.Value = context.Request.QueryString("id")
cmd.Parameters.Add(PersonID)
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
context.Response.ContentType = dr("PersonImageType").ToString()
context.Response.BinaryWrite(CType(dr("PersonImage"), Byte()))
dr.Close()
con.Close()
End Sub


Entire code behind




Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Windows.Forms




Public partial Class Default2
Inherits System.Web.UI.Page
Dim connection As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Dim ds As DataSet = New DataSet()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
If FileUpload1.PostedFile <> Nothing And FileUpload1.PostedFile.FileName <> "" Then
Dim myimage() As Byte = New Byte(FileUpload1.PostedFile.ContentLength) {}
Dim Image As HttpPostedFile = FileUpload1.PostedFile
Image.InputStream.Read(myimage, 0, CType(FileUpload1.PostedFile.ContentLength, Integer))
Dim con As SqlConnection = New SqlConnection(connection)
Dim storepersonDetail As SqlCommand = New SqlCommand("Insert into Person " + "(PersonEmail, Sex, DOB,PersonImage,personImageType) " + " values (@PersonEmail,@Sex,@DOB,@PersonImage,@PersonImageType)",con)
storepersonDetail.Parameters.Add("@PersonEmail", SqlDbType.VarChar, 255).Value = TextBox3.Text
storepersonDetail.Parameters.Add("@Sex", SqlDbType.VarChar, 10).Value = TextBox2.Text
storepersonDetail.Parameters.Add("@DOB", SqlDbType.DateTime).Value = Convert.ToDateTime(TextBox4.Text)
storepersonDetail.Parameters.Add("@PersonImage", SqlDbType.Image, myimage.Length).Value = myimage
storepersonDetail.Parameters.Add("@PersonImageType", SqlDbType.VarChar, 255).Value = FileUpload1.PostedFile.ContentType
con.Open()
storepersonDetail.ExecuteNonQuery()
con.Close()
GridView2.DataSource = FetchAllImagesInfo()
GridView2.DataBind()
GridView2.Visible = True
End If
End Sub

Public Function FetchAllImagesInfo() As DataTable
Dim con As SqlConnection = New SqlConnection(connection)
Dim sql As String = "Select * from person"
Dim da As SqlDataAdapter = New SqlDataAdapter(sql,con)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Function

End Class


Code on handler




HANDLER

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

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration


Public Class Handler
Implements IHttpHandler

Dim Connection As String = ConfigurationManager.ConnectionStrings("conStr").ConnectionString

Public Sub ProcessRequest(ByVal context As HttpContext)
Dim con As SqlConnection = New SqlConnection(Connection)
con.Open()
Dim sql As String = "Select PersonImage, PersonImageType from Person where PersonID=@PersonID"
Dim cmd As SqlCommand = New SqlCommand(sql,con)
Dim PersonID As SqlParameter = New SqlParameter("@PersonID",SqlDbType.Int,0)
PersonID.Value = context.Request.QueryString("id")
cmd.Parameters.Add(PersonID)
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
context.Response.ContentType = dr("PersonImageType").ToString()
context.Response.BinaryWrite(CType(dr("PersonImage"), Byte()))
dr.Close()
con.Close()


End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: