472,954 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Response.Redirect Error in Custom Class

Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on a
..aspx page. Can you not perform response.redirects in a custom class that
is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to the
response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS NULL)
OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception
'************************************************* *************************
'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace
Nov 19 '05 #1
7 2169

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:#4**************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on a
.aspx page. Can you not perform response.redirects in a custom class that is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to the response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS NULL) OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception
'************************************************* ************************* 'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace

Cannot have a redirect in a try loop. You need to set a variable to
true/false in the try loop and then do the redirect outside of the loop.
Nov 19 '05 #2
That is the documented behavior. Response.Redirect has to be outside the Try
block.

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on a
.aspx page. Can you not perform response.redirects in a custom class
that is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to
the response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS
NULL) OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception
'************************************************* *************************
'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace

Nov 19 '05 #3
Ahh! Much appreciated! Thanks.
"vMike" <Mi************@spamnotgewarren.com.delete> wrote in message
news:0Z************@newsread1.news.atl.earthlink.n et...

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:#4**************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on
a
.aspx page. Can you not perform response.redirects in a custom class

that
is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to

the
response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as
RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS

NULL)
OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception

'************************************************* *************************
'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace

Cannot have a redirect in a try loop. You need to set a variable to
true/false in the try loop and then do the redirect outside of the loop.

Nov 19 '05 #4
I added the following lines of code just below the end try and still get the
same error. Any thoughts?
'since try catch can't handle response.redirects, got to have an if then

If blnErrCatch Then

response.redirect("Login.aspx?e=2")

End If

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on a
.aspx page. Can you not perform response.redirects in a custom class
that is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to
the response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS
NULL) OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception
'************************************************* *************************
'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace

Nov 19 '05 #5

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:e3**************@tk2msftngp13.phx.gbl...
I added the following lines of code just below the end try and still get the same error. Any thoughts?
'since try catch can't handle response.redirects, got to have an if then

If blnErrCatch Then

response.redirect("Login.aspx?e=2")

End If

In a share function probably need to refer to response using
HttpContext.Current.Response.
Nov 19 '05 #6
Since you are in a custom class you will need to use the HttpContext.Current
property. The Response property is part of the page, but since you are
outside the scope of your page, this is the proper way to access it. It is
similiar to accessing Session or Application outside of a page class.

HTH,

bill

Try
'validation
Catch ex As Exception
System.Web.HttpContext.Current.Response.Redirect( "Login.aspx?e=1" )
End Try

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a custom class with a public method. I want to perform a
repose.redirect if an error occurs in the public method GetUserRoles.
Unfortunately, Visual Studio 2003 is throwing an error when I try to do
this.

I have imported the System.Web namespace and the same code works fine on a
.aspx page. Can you not perform response.redirects in a custom class that is being called by another aspx page? Please help.

The error I get is: "Name 'Response' is not declared" and it points to the response.redirect line of code.

Can anyone shed some light on this?

Here's the code from the custom class:

Option Strict On
Imports System
Imports System.Collections
Imports System.Data
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.SqlClient
Namespace RoleSecurity
'Public Class UserHelpers
' A static string that contains the path to the XML users file
Public Shared strDbConn As String =
"Server=10.144.125.2;uid=web_user;pwd=xxx;database =Pubs"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' Returns an Array of user roles for a given username
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Public Shared Function GetUserRoles(ByVal username As String) As String()
' Create a string to persist the roles
Dim roleStr() As String
Dim userRoles As New ArrayList
Dim conDB As SqlConnection
Dim cmdSelectRoles As SqlCommand
Dim dtrRoles As SqlDataReader
'Create db conn object
conDB = New SqlConnection(strDbConn)
Try
'open db connection
conDB.Open()
cmdSelectRoles = New SqlCommand("SELECT UPPER([Role_Code_Txt]) as RoleCode
FROM([SpartanMotors].[dbo].[Master_Users_Roles_X_Ref_Tbl]) " & _
"WHERE (LOWER(User_ID_Txt) = LOWER('rigs')) AND ( (InActiveOn_Date IS NULL) OR (GETDATE() < InActiveOn_Date) ) ", conDB)
dtrRoles = cmdSelectRoles.ExecuteReader()
While dtrRoles.Read()
userRoles.Add(dtrRoles("RoleCode"))
End While
dtrRoles.Close()
conDB.Close()
Catch ex As Exception
'************************************************* ************************* 'Error occurs on line below
Response.Redirect("Login.aspx?e=1")
'************************************************* ************************
End Try
roleStr = CType(userRoles.ToArray(GetType(String)), String())
Return roleStr
End Function ' GetUserRoles

End Class

End Namespace

Nov 19 '05 #7
That was it! thanks a million!

"vMike" <Mi************@spamnotgewarren.com.delete> wrote in message
news:8q*************@newsread1.news.atl.earthlink. net...

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:e3**************@tk2msftngp13.phx.gbl...
I added the following lines of code just below the end try and still get

the
same error. Any thoughts?
'since try catch can't handle response.redirects, got to have an if then

If blnErrCatch Then

response.redirect("Login.aspx?e=2")

End If

In a share function probably need to refer to response using
HttpContext.Current.Response.

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: TinyTim | last post by:
I'm a newbie at ASP & HTML. It seems that when you use server side code and you're going to return a customized HTML form with several fields and labels, you have to do an extensive amount of...
11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
5
by: john | last post by:
Hello, One our thrid page for some reason the response.redirect isn't working on the live server. It works fine on the development machine but when we move the code to the live server it doesn't...
2
by: hansiman | last post by:
Why can't I use Response.Redirect in a class.vb file? /M
1
by: Mukund Patel | last post by:
Hi friends, I have implemented custom error handling at page level and application level. If there is any syntax error in my aspx class file it will redirect to error page when I run the page....
1
by: MikeM | last post by:
We are getting a behavior on a Response.Redirect("SomeUrl", True) that I'm hoping someone can explain. This all refers to the code snip at the end. By the way, this is all VB ASP.NET v1.0 code. ...
1
by: ocbka1 | last post by:
i'm using creating a webpage on the fly that i save as an xls file to be attached to an email and sent dynamically. i've got a custom response filter class to write it out. the problems start...
12
by: Jim Rodgers | last post by:
I have a big asp file that has an error under certain conditions -- totally repeatable. However, it only fails when I set response.buffer = True at the top. WHen I set it False in order to debug...
9
by: Nick | last post by:
Hi there, I would like to perform something like the following from my vb.net web service, being invoked via HTTP Post Call HttpContext.Current.Response.Redirect("myprotocol://myurl") ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.