Solved

Response is not available in this context.

Posted on 2010-11-23
8
802 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 32
looking for an ASP.net report designer 4 40
How to repeat the data 4 30
Find date of 2nd Thursday of each month 3 31
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
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…

679 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