Response.Redirect Error in Custom Class

J

Joe Rigley

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
 
V

vMike

Joe Rigley said:
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.
 
J

Joe Rigley

Ahh! Much appreciated! Thanks.


vMike said:
Joe Rigley said:
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.
 
J

Joe Rigley

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
 
V

vMike

Joe Rigley said:
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.
 
W

William F. Robertson, Jr.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top