Solved

asp.net Response.Redirect Now Working

Posted on 2004-08-18
38
495 Views
Last Modified: 2008-02-01
I've got a header include file that contains the code.

I have another in line code module in the same app where the following redirect code works great, but in this file it won't work.  The select statement is working in the other file.

I can't figure out how to test this module because the code is in a code behind file called forum.aspx.vb.  

This is the code:

Imports System
Imports System.Data
Imports System.Data.OleDB
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports dotForumIncludes

Namespace dotForumClientTopic

Public Class viewAllRecords : Inherits System.Web.UI.Page

      Public myIncludes As New includeFunctions

      Protected dotForumDisplay As System.Web.UI.WebControls.DataGrid
      Protected messageCenter As System.Web.UI.HtmlControls.HtmlGenericControl
      Protected threadLink As System.Web.UI.HtmlControls.HtmlGenericControl
      
      Public pageTitle As String = "BCA Members Area"
      Public pageDesc As String = "BCA Bulletin Board"
      Public pageKeys As String = "clinic management, software, IT Services"

Sub Page_Load(sender as Object, e as EventArgs)

Dim MyConnection As SqlConnection
Dim strRole as string

if not Page.IsPostBack then

      Dim strConn as string = "server=(local);database=BCA;Trusted_Connection=yes"
      Dim sql as string = "SELECT Role FROM Users WHERE (UserName = '" + Session("Username") +"')"
      Dim conn as New SQLConnection(strConn)
      Dim Cmd as New SQLCommand(sql, conn)
      Dim objDR as SQLDataReader
      conn.Open()
      objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
      While objDR.Read()
        
         strRole=objDr("Role")
         End While
if not Page.IsPostBack And strRole="Admin" then
Response.redirect("forum2.aspx")
Else
bindData()
      
End If
End If

End Sub

Why isn't it redirecting?
0
Comment
Question by:nhorsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 20
  • 14
  • 2
  • +2
38 Comments
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835174
Don't you need to add
"Handles MyBase.Load" onto the Page_Load sub?  Thus it becomes:
Sub Page_Load(sender as Object, e as EventArgs) Handles MyBase.Load


Also try this changing this:
if not Page.IsPostBack And strRole="Admin" then
Response.redirect("forum2.aspx")
Else
bindData()
     
End If
 
TO:


if Trim(strRole)="Admin" then
Response.redirect("forum2.aspx")
Else
bindData()
End If

-Thrill
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835281
I added both your suggestions and it blew right by the code.  It's acting like it's not running the page_load code.  But that's not possible, right?
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835296
I checked and the Session("Username") is working.  How can I check to see if the strRole is bringing back a value?

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Author Comment

by:nhorsley
ID: 11835308
I'm raising the points.  This is obviously harder than I originally thought.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835309
Try running your query in query analyzer, make sure it really is returning only 1 value, and its the correct value.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835354
I can't use the session value but I can use one username and when I use that criteria the query is returning one value and it is the correct value.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835388
Try adding a label to the page, and add this:
if Trim(strRole)="Admin" then
Label1.Text = "inside if"
Response.redirect("forum2.aspx")
Else
Label1.Text = "else if"
bindData()
End If


Just to see that it is even getting inside of the page load.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835507
I put in the label and the code and the label is not populated.  I tried the label in a couple of places.  It looks like the strRole value is not being found.  How can I check that?
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835528
If nothing is showing up, the entire method is not being called.  

What happens when you change this:
Sub Page_Load(sender as Object, e as EventArgs)
to
Sub Page_Load(sender as Object, e as EventArgs) Handles MyBase.Load

Because I really think you need that Handle...
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835580
I made that change when you suggested it and it made no difference.  It's running with that now.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835602
It's weird.  It's like the label is not being seen but when the page loaded and I have originally made a mistake in the label tag, it threw an error.  Now the label loads but it won't pass in the value from the vb code.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835605
Try taking off this:
if not Page.IsPostBack then
and any references to this.  And see if those labels are filling up yet.  If they aren't.  Its got to be something wrong with your html code.  Your code behind must not be linked up properly with your client side html.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835699
I took the page.ispostback out and nothing changed.  I think you're right about there being a break between the code behind and the html.  I'm using an online forum that I've customized.  The html page calls an include that contains html as well as a code behind file that contains code.  Want me to post all three files?
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835721
No, just make sure that you have a line of code like this on top of your html code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="PAGENAME.aspx.vb" Inherits="MasterMembership.PAGENAME"%>


Then rebuild the project.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835736
These are the tags at the top of the .aspx page:

<%@ Page language="VB" Inherits="dotForumClientTopic.viewAllRecords" %>

<!--#include virtual="includes/clientHeader.inc.asp"-->

<script language="VB" runat="server">
</script>
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 11835752
This part is very important
Codebehind="PAGENAME.aspx.vb"

So change it to:
<%@ Page language="VB" Codebehind="viewAllRecords.aspx.vb" Inherits="dotForumClientTopic.viewAllRecords" %>

and the code behind page should be called viewAllRecords.aspx.vb...
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835754
This is the code at the start of the code behind file forum.aspx.vb:

Imports System.Data.OleDB
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports dotForumIncludes

Namespace dotForumClientTopic

Public Class viewAllRecords : Inherits System.Web.UI.Page

      Public myIncludes As New includeFunctions

      Protected dotForumDisplay As System.Web.UI.WebControls.DataGrid
      Protected messageCenter As System.Web.UI.HtmlControls.HtmlGenericControl
      Protected threadLink As System.Web.UI.HtmlControls.HtmlGenericControl
      
      Public pageTitle As String = "BCA Members Area"
      Public pageDesc As String = "BCA Bulletin Board"
      Public pageKeys As String = "clinic management, software, IT Services"

Sub Page_Load(sender as Object, e as EventArgs) Handles MyBase.Load
ETC.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835763
or I guess Codebehind="forum.aspx.vb" ...
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835791
ViewAllRecords is the name of the class.  The code is firing perfectly (except it won't redirect) and the code behind file is named forum.aspx.vb

I'll rename it.  But this code behind stuff is really a pain.  I'll be glad when VS 2005 fixes it.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835832
I changed the name and the pages still fire perfectly, just no label in view.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11835853
To tell if its firing or not (which to me it looks like its not)...

Immediately after the beginning of pageload (first line)
add
Label1.Text = "Here!!!"

If that label shows up with the proper text, your pageload event is firing.  If it doesn't show up, the event it not firing.

0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835895
Nothing.  But the page_load event must be firing because the page loads.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11835905
Is it loading from cache?  Or what?
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 11835935
The page doesn't need a page_load event to fire in order for the page to load.  This was proven because you didn't tell the html page where the codebehind file was...  If you don't specifically say where the code behind page is, it just doesn't use it.  If the on pageload event is still not firing, again the problem lies with the html code.

In other words, if no page behind is given (or its incorrect), it just doesn't use it, and the html page just loads up, with no server side code.  Just client side html.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11836033
But the page is loading with the DataBind method populating a datagrid.  That couldn't happen unless the codebehind file was firing as well.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11836049
Ok, then I don't know exactly what is happening...  If you want you can post the html side code.  Just so we can check that.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11836068
The original page tag was <%@ Page language="VB" Inherits="dotForumClientTopic.viewAllRecords" %>

I suspect the default code behind page is the same as the aspx page, in this case forum.aspx has the code behind page named forum.aspx.vb.  And that's what the original file was named.  When I changed the name as you suggested it still fired the same code as before.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11836079
the Inherits tag called this code:

Namespace dotForumClientTopic

Public Class viewAllRecords : Inherits System.Web.UI.Page
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11836093
Ok, just try this for me...
Immediately after the on pageload event is called
add this line of code: Response.redirect("anywhere.aspx")

so its like this...

Sub ....
   Response.redirect("anywhere.aspx")


End sub

I just want to make sure it is not getting fired...  If you get the redirect it is firing.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11836139
It did not redirect and put the line of code immediately after the Sub OnPage_load
0
 
LVL 8

Expert Comment

by:thrill_house
ID: 11836164
So I have no idea how the Datagrid is getting bound... because it sure isn't getting bound from within the pageload event.

You are going to have to figure out how the datagrid is getting bound.  Once you find that you might see whats going wrong here.
I'm nearly done with work now, so I won't be around to answer any more questions until tomorrow morning.  Good luck.
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11836193
Thank you.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
ID: 11836225
1 last thing before I go...
Its possible that the problem is that the class name is different than the name of the file.
I know in java, the class name MUST be the same name as the file (minus the extension)...
So your class name should be forum
0
 
LVL 21

Assisted Solution

by:tovvenki
tovvenki earned 20 total points
ID: 11840094
Hi,
when the query just returns only one value why are you using a while loop just use the if condition change the following
 While objDR.Read()
       
        strRole=objDr("Role")
        End While

to

if objDR.Read() then
        strRole=objDr("Role")
End if

Regards,
venki
0
 
LVL 9

Assisted Solution

by:hismightiness
hismightiness earned 160 total points
ID: 12069881
After reading this entire thread, I just wanted to clear something up.  The Codebehind="file.aspx.vb" is NOT very important to anything except Visual Studio.  The framework does not use this at all.  It is only for Visual Studio to know where to locate the file.  What IS important and explains why the binding was still occuring, is that the Inherits="class" is correct.  Without that correctly stated and a matching class, your page will not even be rendered.  You will get a compiler error in the browser.

Finally, I doubt this will help but change the reference to the DataReaderItem:

        Dim MyConnection As SqlConnection
        Dim strRole As String

        If Not Page.IsPostBack Then

            Dim strConn As String = "server=(local);database=BCA;Trusted_Connection=yes"
            Dim sql As String = "SELECT Role FROM Users WHERE (UserName = '" + Session("Username") + "')"
            Dim conn As New SqlConnection(strConn)
            Dim Cmd As New SqlCommand(sql, conn)
            Dim objDR As SqlDataReader
            conn.Open()
            objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
            if objDR.HasRows then
                        While objDR.Read()
                            strRole = CType(objDR.Item("Role"), String) 'right here
                        End While
            else
                        Throw New Exception("SqlDataReader is empty - The role was not found")
            end if
            If Not Page.IsPostBack And strRole = "Admin" Then
                Response.Redirect("forum2.aspx")
            Else
                bindData()
            End If
        End If

I also added an IF statement to check to see if the DataReader is even returning anything.  From there, you might be led in the right direction to figuring out why this isn't working, or maybe not.  Try it out...  :)
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 100 total points
ID: 12081928
Hello,

Few questions:
1. Are you compiling and running the project on your local machine?
2. If yes then, why dont you debug it by attaching break points in code behind rather than putting these weird Labels etc. as suggested by almost everyone else here. If you dont know how to do that, let me know I will tl you how to.

warm regards,
Ajit Anand
0
 
LVL 9

Accepted Solution

by:
hismightiness earned 160 total points
ID: 12083660
Nice advice, Ajit.  I completely looked over that, because I really wanted to clear up that misconception.  Debugging or Tracing are both much better ways to "debug" your code.  Personally, I prefer tracing - but that is just me.  :)
0
 
LVL 1

Author Comment

by:nhorsley
ID: 12083688
Thanks for attempting to help.  I solved the problem by removing the need to redirect.  I will divide the points and end the question.
0

Featured Post

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!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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