Solved

Response is not available in this context.

Posted on 2010-11-23
8
795 Views
Last Modified: 2012-06-22
Hi Experts,

I call a subroutine in a class to set some session variables, the following code is within a try, however, an exception is raised (Response is not available in this context.) when I try to redirect to this page Response.Redirect("CreateNewProject.aspx"), why is this and what am I doing wrong?

Try
            'create new dataset to read the xml in the customer config table into.
            Dim dsxml As New DataSet
            dsxml.ReadXml(Session.Item("customerConfigFilePath"))
            Dim userNameStr As String = "UserName = '" & userName & "'"

            'search for the user row based on thier username
            Dim dataRows As DataRow() = dsxml.Tables(0).Select(userNameStr)

            If dataRows(0).Item("NumberOfLogins") = 1 Then
                Response.Redirect("CreateNewProject.aspx")
            Else
                'set session variables
            Endif

 Catch ex As Exception
            l.writeToLogFile(Session.Item("customerLogFile"), "[Error #0031] Could not setup the session variables " & ex.Message.ToString)
        End Try
0
Comment
Question by:MOSSPOINT
  • 4
  • 4
8 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 34197593
If you need access to the Response object outside the Page, use this:
HttpContext.Current.Response
0
 

Author Comment

by:MOSSPOINT
ID: 34197676
I'm not sure I understand, could you re-write the code example I included with your idea to show me how to do it?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34197705
The section of code to be modified:

            If dataRows(0).Item("NumberOfLogins") = 1 Then
                ''Use HttpContext.Current.Response here:
                HttpContext.Current.Response.Redirect("CreateNewProject.aspx")
            Else
                'set session variables
            Endif

More information here:
"Working With HttpContext.Current"
http://odetocode.com/Articles/112.aspx
0
 

Author Comment

by:MOSSPOINT
ID: 34197843
Eric,

I made that change and now I get the following exception, any ideas?

System.Threading.ThreadAbortException = {Property evaluation failed.}
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 32

Expert Comment

by:Erick37
ID: 34198002
It appears that we need to set the second parameter of Redirect to False.
Give this a try:

            If dataRows(0).Item("NumberOfLogins") = 1 Then
                ''Use HttpContext.Current.Response here:
                HttpContext.Current.Response.Redirect("CreateNewProject.aspx", False)
            Else
                'set session variables
            Endif

Documentation
http://support.microsoft.com/kb/312629/EN-US/ 
0
 

Author Comment

by:MOSSPOINT
ID: 34200641
Eric,

The line HttpContext.Current.Response.Redirect("CreateNewProject.aspx", False) does not throw an exception this time but neither does it redirect to the CreateNewProject.aspx page.

Any ideas?
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 34201974
Not sure why, I was able to redirect using the following.
The class code follows.  Notice I have 2 ways to redirect, Redirect1 uses the Page.Response.Redirect() and Redirect2 uses HttpContext.Current.Response.Redirect().  Both seem to work for me in my sample application.

Utilities.Redirect1(Me.Page)

and

Utilities.Redirect2()


Public Class Utilities



	Public Shared Sub Redirect1(ByVal page As Page)



		page.Response.Redirect("webform1.aspx", False)



	End Sub



	Public Shared Sub Redirect2()



		HttpContext.Current.Response.Redirect("webform1.aspx", False)



	End Sub



End Class

Open in new window

0
 

Author Closing Comment

by:MOSSPOINT
ID: 34203244
Thanks
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now