Solved

asp.net Response.Redirect Now Working

Posted on 2004-08-18
38
489 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
  • 20
  • 14
  • 2
  • +2
38 Comments
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I checked and the Session("Username") is working.  How can I check to see if the strRole is bringing back a value?

0
 
LVL 1

Author Comment

by:nhorsley
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
or I guess Codebehind="forum.aspx.vb" ...
0
 
LVL 1

Author Comment

by:nhorsley
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:nhorsley
Comment Utility
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
Comment Utility
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
Comment Utility
Nothing.  But the page_load event must be firing because the page loads.
0
 
LVL 1

Author Comment

by:nhorsley
Comment Utility
Is it loading from cache?  Or what?
0
 
LVL 8

Expert Comment

by:thrill_house
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It did not redirect and put the line of code immediately after the Sub OnPage_load
0
 
LVL 8

Expert Comment

by:thrill_house
Comment Utility
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
Comment Utility
Thank you.
0
 
LVL 8

Assisted Solution

by:thrill_house
thrill_house earned 220 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

11 Experts available now in Live!

Get 1:1 Help Now