By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,397 Members | 2,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,397 IT Pros & Developers. It's quick & easy.

How to store image in sesstion ?

P: n/a
Dear All,Here I am facing problem to store image.I am trying to store byte
array(image) in to session variable so at time of update I will got that
byte array and I do my update. here i am given following code so kindly help
out me. on Button Click Event---------------------------------
Private Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click
' Dim fileUpload1 As FileUpload =
CType(Me.FindControl("fpPhoto"), FileUpload)
'Make sure a file has been successfully uploaded
If fpPhoto.PostedFile Is Nothing OrElse
String.IsNullOrEmpty(fpPhoto.PostedFile.FileName) OrElse
fpPhoto.PostedFile.InputStream Is Nothing Then
lblerror.Text = "Please Upload Valid picture file"
Exit Sub
End If
Session("Path") = fpPhoto.PostedFile.FileName.ToString
'Make sure we are dealing with a JPG or GIF file
Dim extension As String =
System.IO.Path.GetExtension(fpPhoto.PostedFile.Fil eName).ToLower()
Dim MIMEType As String = Nothing
Select Case extension
Case ".gif"
MIMEType = "image/gif"
Case ".jpg", ".jpeg", ".jpe"
MIMEType = "image/jpeg"
Case ".png"
MIMEType = "image/png"
Case Else
'Invalid file type uploaded
Label1.Text = "Not a Valid file format"

Exit Sub
End Select

ImgPhoto.ImageUrl = fpPhoto.PostedFile.FileName
'Connect to the database and insert a new record into Products
Dim imageBytes(fpPhoto.PostedFile.InputStream.Length) As Byte
fpPhoto.PostedFile.InputStream.Read(imageBytes, 0,
Photo = imageBytes

' Session("Photo") = Photo

Catch ex As Exception
Throw ex
End Try
End Sub
update called by following
Photo = CType(Session("Photo"), Byte())
UpdatePH_ID(Session("PH_ID_NO"), Session("DistID"), Photo)

member Variable
Private mPhoto As Byte() = Nothing
Public Property Photo() As Byte()
Return mPhoto
End Get
Set(ByVal Value() As Byte)
mPhoto = Value
End Set
End Property
Public Sub UpdatePH_ID(ByVal id As Integer, ByVal id1 As Integer, ByVal p As
'Make the object of the class that is supporting this Web Service

'Make the dataset that is to be used thoughout here
Dim objParameter As New cParameter(ds)

'Make the objParameter ready for insert
getCollection(objParameter, 2, id, id1, p, False)
'Make the dataset ready by filling it
If objservices.UpdateSJEDPH_ID1PhotoData(ds) = 1 Then
MsgBox("Photo for the current document is updated!")
End If

End Sub
Private Sub getCollection(ByRef objParameter As cParameter, ByVal index
As Int16, ByVal id As Integer, ByVal id1 As Integer, ByVal p As Byte(),
Optional ByVal common As Boolean = False)
If IsNothing(objParameter) Then
objParameter = New cParameter(ds)
End If
'objParameter.CreateParameter(dsEntry, "UserID",
'objParameter.CreateParameter(dsEntry, "OfficeID",
'objParameter.CreateParameter(dsEntry, "RoleID",

Dim param As SqlParameter() = New SqlParameter(2) {}

param(0) = New SqlClient.SqlParameter("@PH_ID_NO1", ID)
param(1) = New SqlClient.SqlParameter("@dist_cd", id1)
param(2) = New SqlClient.SqlParameter("@Photo", p)

Dim drEntryPage As DataRow
ds.Tables("Parameter").Columns.Add("Photo", GetType(Byte()))
ds.Tables("Parameter").Columns.Add("Thumb", GetType(Byte()))
drEntryPage = ds.Tables("Parameter").NewRow()

End Sub
Private Sub SetValue(ByRef drEntryPage As DataRow)
With drEntryPage
'.Item("SrNo") = DBNull.Value
'.Item("DocumentID") = DBNull.Value
'.Item("DocumentRefID") = DBNull.Value
'.Item("PhotoThumbID") = strPK
.Item("PName") = "Photo"
.Item("PValue") = Photo
.Item("PType") = enmParameterType.Input
.Item("DBType") = SqlDbType.Image
.Item("Photo") = Photo

'.Item("UserID") = My.Application.UserInfo.UserID
'.Item("OfficeID") = My.Application.UserInfo.OfficeID
'.Item("RoleID") = My.Application.UserInfo.RoleID
End With
Catch ex As Exception
Throw ex
End Try
End Sub

web services
Public Function UpdateSJEDPH_ID1PhotoData(ByRef ds As DataSet) As
Dim param As SqlParameter() = Nothing
Dim id As Integer
Dim i As Integer
param = New SqlParameter(ds.Tables(0).Rows.Count - 1) {}
For i = 0 To ds.Tables("Parameter").Rows.Count - 1
If Not IsDBNull(ds.Tables(0).Rows(i).Item(1)) AndAlso
ds.Tables(0).Rows(i).Item("DBType").ToString = "Image" Then
param(i) = New
SqlParameter(ds.Tables(0).Rows(i).Item(0).ToString ,
param(i) = New
SqlParameter(ds.Tables(0).Rows(i).Item(0).ToString ,
End If
Next i
id = SqlHelper.ExecuteScalar(getConnectionString(),
CommandType.StoredProcedure, "UpdateSJEDPH_ID1PhotoData", param)
Return id
End Function
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.