By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

Response.Redirect Error in Custom Class

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.