Solved

Response is not available in this context.

Posted on 2010-11-23
8
789 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
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…

757 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

23 Experts available now in Live!

Get 1:1 Help Now