asp.net Response.Redirect Now Working

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?
LVL 1
nhorsleyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thrill_houseCommented:
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
nhorsleyAuthor Commented:
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?
nhorsleyAuthor Commented:
I checked and the Session("Username") is working.  How can I check to see if the strRole is bringing back a value?

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

nhorsleyAuthor Commented:
I'm raising the points.  This is obviously harder than I originally thought.
thrill_houseCommented:
Try running your query in query analyzer, make sure it really is returning only 1 value, and its the correct value.
nhorsleyAuthor Commented:
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.
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
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?
thrill_houseCommented:
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...
nhorsleyAuthor Commented:
I made that change when you suggested it and it made no difference.  It's running with that now.
nhorsleyAuthor Commented:
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.
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
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?
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
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>
thrill_houseCommented:
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...
nhorsleyAuthor Commented:
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.
thrill_houseCommented:
or I guess Codebehind="forum.aspx.vb" ...
nhorsleyAuthor Commented:
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.
nhorsleyAuthor Commented:
I changed the name and the pages still fire perfectly, just no label in view.
thrill_houseCommented:
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.

nhorsleyAuthor Commented:
Nothing.  But the page_load event must be firing because the page loads.
nhorsleyAuthor Commented:
Is it loading from cache?  Or what?
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
But the page is loading with the DataBind method populating a datagrid.  That couldn't happen unless the codebehind file was firing as well.
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
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.
nhorsleyAuthor Commented:
the Inherits tag called this code:

Namespace dotForumClientTopic

Public Class viewAllRecords : Inherits System.Web.UI.Page
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
It did not redirect and put the line of code immediately after the Sub OnPage_load
thrill_houseCommented:
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.
nhorsleyAuthor Commented:
Thank you.
thrill_houseCommented:
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
tovvenkiCommented:
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
hismightinessCommented:
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...  :)
ajitanandCommented:
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
hismightinessCommented:
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.  :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nhorsleyAuthor Commented:
Thanks for attempting to help.  I solved the problem by removing the need to redirect.  I will divide the points and end the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.