Solved

Response is not available in this context.

Posted on 2010-11-23
8
798 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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
 
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

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.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

770 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