Code blowing up on Session

Hi,

Trying to do a simple pass variable in a session in an ashx handler.

The code blows up getting the session

on this line

 Dim IncidentReportID = context.Session("IncidentReportID")
line 246

seems simple enough thing to do. don't know why it's crashing
 
.vb 
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Session("IncidentReportID") = Context.Request.QueryString("ID")

    End Sub


ashx

<%@ WebHandler Language="VB" Class="UploadHander" %>

Imports System
Imports System.Web
Imports System.IO
Imports System.Drawing
Imports System.Data
Imports System.Data.SqlClient
Imports ElementIT.UltimateUploader

Imports System.Net



Public Class UploadHander : Implements IHttpHandler
    Private savePath As String = "~/Data/IncidentPhotos/Picture"
    Private saveThumbnaiPath As String = "~/Data/IncidentPhotos/PictureThumb"
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
        
        Dim Dal As New Dal
        
        
        Dim fileName As String = context.Request.QueryString("FileName")
        Dim fileComment As String = context.Request.QueryString("Comment")
        Dim tag As String = context.Request.QueryString("Tag")
        fileName = HttpUtility.UrlDecode(fileName).Replace("..\", "")
        Dim complete As Boolean = If(String.IsNullOrEmpty(context.Request.QueryString("Complete")), True, Boolean.Parse(context.Request.QueryString("Complete")))
        Dim querySize As Boolean = If(String.IsNullOrEmpty(context.Request.QueryString("QuerySize")), False, Boolean.Parse(context.Request.QueryString("QuerySize")))
        Dim startByte As Long = If(String.IsNullOrEmpty(context.Request.QueryString("StartByte")), 0, Long.Parse(context.Request.QueryString("StartByte")))
	


        If String.IsNullOrEmpty(fileName) OrElse String.IsNullOrEmpty(savePath) Then
            Return
        End If
        Dim dirThumbpath As String = saveThumbnaiPath
        Dim dirPath As String = savePath
        Dim filePath As String
        Dim fileThumbPath As String

        If savePath.StartsWith("/") OrElse savePath.StartsWith("~") Then
            Try
                dirPath = context.Server.MapPath(savePath)
            Catch
            End Try
        End If


        If saveThumbnaiPath.StartsWith("/") OrElse saveThumbnaiPath.StartsWith("~") Then
            Try
                dirThumbpath = context.Server.MapPath(saveThumbnaiPath)
            Catch
            End Try
        End If


        filePath = System.IO.Path.Combine(dirPath, fileName)

        fileThumbPath = System.IO.Path.Combine(dirThumbpath, fileName)

        If querySize Then
            If Not System.IO.Directory.Exists(dirPath) Then
               
                Return
            End If

            Dim fi As New System.IO.FileInfo(filePath)
            If Not fi.Exists Then
                
            Else
               
            End If
            
        Else
            Dim fs As System.IO.FileStream = Nothing
            Try
                If startByte > 0 AndAlso System.IO.File.Exists(filePath) Then
                    fs = System.IO.File.Open(filePath, System.IO.FileMode.Append)
                    SaveFile(context.Request.InputStream, fs)
                    fs.Close()
                    Using fs2 As New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
                        Try
                            Using bitmap As Bitmap = DirectCast(System.Drawing.Image.FromStream(fs2, True, False), Bitmap)
                                Try
                                    Dim pThumbnail As System.Drawing.Image = bitmap.GetThumbnailImage(100, 100, Nothing, New IntPtr())
                                    ' pThumbnail.Save(fileThumbPath);
                                    'bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);


                                    Dim OrigWith = bitmap.Width
                                    Dim OrigHeight= bitmap.Height

                                    'Dim newWidth As Integer = "100"
                                    'Dim newHeight As Integer = "100"

                                    Dim imgHeight As Integer = 0
                                    Dim imgWidth As Integer = 0
                                    Dim ScaledImageWidth As Integer = 0
                                    Dim ScaledImageHeight As Integer = 0

                                    'Enter size to scale thumbnail too
                                    Dim MaxWidth As Integer = 250
                                    Dim MaxHeight As Integer = 250

                                    imgHeight = OrigHeight
                                    imgWidth = OrigWith

                                    ' Determine Scaling
                                    If imgWidth > MaxWidth Or imgHeight > MaxHeight Then
                                        'Determine what dimension is off by more
                                        Dim deltaWidth As Integer = imgWidth - MaxWidth
                                        Dim deltaHeight As Integer = imgHeight - MaxHeight
                                        Dim scaleFactor As Double = 0

                                        If deltaHeight > deltaWidth Then
                                            'Scale by the height
                                            scaleFactor = MaxHeight \ imgHeight
                                        Else
                                            'Scale by the Width
                                            scaleFactor = MaxWidth \ imgWidth
                                        End If




                                        imgWidth *= CInt(Math.Truncate(scaleFactor))
                                        imgHeight *= CInt(Math.Truncate(scaleFactor))
                                        If scaleFactor = 0 Then
                                            imgWidth = 250
                                            imgHeight = 250

                                        End If
                                    End If

                                    ScaledImageWidth = imgWidth
                                    ScaledImageHeight = imgHeight

                                    Dim thumbnail As New Bitmap(ScaledImageWidth, ScaledImageHeight)

                                    Dim Graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumbnail)
                                    Graphic.DrawImage(bitmap, 0, 0, ScaledImageWidth, ScaledImageHeight)

                                    thumbnail.Save(fileThumbPath)


                                    GC.Collect()
                                Catch ex As Exception
                                    Throw ex
                                End Try
                            End Using
                        Catch aex As ArgumentException
                            Throw New Exception("The file received from the Map Server is not a valid jpeg image", aex)
                        End Try
                    End Using
                  
                Else
                    fs = System.IO.File.Create(filePath)
                    SaveFile(context.Request.InputStream, fs)
                    fs.Close()
                    Using fs2 As New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
                        Try
                            Using bitmap As Bitmap = DirectCast(System.Drawing.Image.FromStream(fs2, True, False), Bitmap)
                                Try
                                    'bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

                                    Dim pThumbnail As System.Drawing.Image = bitmap.GetThumbnailImage(100, 100, Nothing, New IntPtr())
                                    '  pThumbnail.Save(fileThumbPath);
                                    'System.Drawing.Graphics.DrawImage(                                       pThumbnail,
                                    '   10,
                                    '   10,
                                    '   pThumbnail.Width,
                                    '   pThumbnail.Height);



                                    Dim OrigWith = bitmap.Width
                                    Dim OrigHeight = bitmap.Height

                                    'Dim newWidth As Integer = "100"
                                    'Dim newHeight As Integer = "100"

                                    Dim imgHeight As Integer = 0
                                    Dim imgWidth As Integer = 0
                                    Dim ScaledImageWidth As Integer = 0
                                    Dim ScaledImageHeight As Integer = 0

                                    'Enter size to scale thumbnail too
                                    Dim MaxWidth As Integer = 250
                                    Dim MaxHeight As Integer = 250

                                    imgHeight = OrigHeight
                                    imgWidth = OrigWith

                                    ' Determine Scaling
                                    If imgWidth > MaxWidth Or imgHeight > MaxHeight Then
                                        'Determine what dimension is off by more
                                        Dim deltaWidth As Integer = imgWidth - MaxWidth
                                        Dim deltaHeight As Integer = imgHeight - MaxHeight
                                        Dim scaleFactor As Double = 0

                                        If deltaHeight > deltaWidth Then
                                            'Scale by the height
                                            scaleFactor = MaxHeight \ imgHeight
                                        Else
                                            'Scale by the Width
                                            scaleFactor = MaxWidth \ imgWidth
                                        End If



                                        imgWidth *= CInt(Math.Truncate(scaleFactor))
                                        imgHeight *= CInt(Math.Truncate(scaleFactor))
                                        If scaleFactor = 0 Then
                                            imgWidth = 250
                                            imgHeight = 250

                                        End If
                                    End If

                                    ScaledImageWidth = imgWidth
                                    ScaledImageHeight = imgHeight

                                    Dim thumbnail As New Bitmap(ScaledImageWidth, ScaledImageHeight)

                                    Dim Graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumbnail)
                                    Graphic.DrawImage(bitmap, 0, 0, ScaledImageWidth, ScaledImageHeight)

                                    thumbnail.Save(fileThumbPath)

                                    Dim PicturePath = "/Data/IncidentPhotos/Picture/" & fileName
                                    Dim ThumbPath = "/Data/IncidentPhotos/PictureThumb/" & fileName

                                    
                                    Dim IncidentReportID = context.Session("IncidentReportID")
        
                                    Dal.InsertIncidentPhotos(IncidentReportID, PicturePath, ThumbPath)
        
                                    
                                    

                                    GC.Collect()
                                Catch ex As Exception
                                    Throw ex
                                End Try
                            End Using
                        Catch aex As ArgumentException
                            Throw New Exception("The file received from the Map Server is not a valid jpeg image", aex)
                        End Try
                    End Using
                  
                End If
                If complete Then
                  
                    ' Place here the code making postprocessing of the uploaded file (moving to other location, database, etc). 
                  
                End If

                
            Catch generatedExceptionName As Exception
                
                
                
            Finally
                If fs IsNot Nothing Then
                    fs.Close()
                End If
            End Try
        End If
     
    End Sub

    Private Sub SaveFile(ByVal stream As Stream, ByVal fs As FileStream)
        Dim buffer(4096) As Byte
        Dim bytesRead As Integer
        bytesRead = stream.Read(buffer, 0, buffer.Length)
        Do While bytesRead <> 0
            fs.Write(buffer, 0, bytesRead)
            bytesRead = stream.Read(buffer, 0, buffer.Length)
        Loop
        
        '  Dim GetImage As System.Drawing.Image = System.Drawing.Image.FromStream(fs)
        fs.Close()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Open in new window

TheWebGuy38Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
You need to inherit IRequiresSessionState:

Public Class UploadHander : Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState

0
Alfred A.Commented:
Oops.  I didn't noticed it is VB.

Public Class UploadHander
       Implements IHttpHandler
       Implements System.Web.SessionState.IRequiresSessionState
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheWebGuy38Author Commented:
THERE WE GO!

worked, and learned something new!

dunno why the context.response.querystrings were working before
0
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.