Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Login Destination off of Table Value

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
Hello All!

I am working on a project and I need the destinationurl of my login control to change depending on a value in a table.

I have it setup for binary configuration currently. Meaning that if a user has special rights to a page its 1 if not its 0.

What i am wanting to do is if the value in the table is 1 then go to AdminInventory.aspx and if it is 0 (or not 1) then go to UserInventory.aspx.

The table is UserInfo and the column is Admin.

My question is how can I do this? Do I do it on the login click of the Login Control or can I call an event under the "DestinationPageUrl"?

Code:
    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'If Username Admin status does not = 1 then
            Login.DestinationPageUrl = "~/Users/UserInventory.aspx"
        'ElseIf username admin status = 1 then
            Login.DestinationPageUrl = "~/Admin/AdminInventory.aspx"
        'End If
    End Sub

I know that code isn't programmatically right I just typed out what i thought I need to make the program do.
 
I don't use the login controls, but it seems like you can do it. What you will have to do is query the database after the user logs in and is authenticated.
 
Would that be an event? Do you have a link for reference or something among that course?
 
I figured some of it out.

I can program on the "loggedin" event the destination URL. Then just called a redirect or something to activate that.

Now I just need to figure out how to go to the table "UserInfo" and pull the value of the "Admin" column. Whether it be a 0 or a 1.
 
I think I have made progess. However I think I am stuck again.

Here is what I have:

Code:
    Protected Sub Login_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim AdminLblRef As Label = CType(dlAdmin.FindControl("AdminLabel"), Label)
        Dim AdminLblText As String = AdminLblRef.Text
        
        
        If AdminLblText = "0" Then
            Login.DestinationPageUrl = "~/Users/UserInventory.aspx"
        ElseIf AdminLblText = "1" Then
            Login.DestinationPageUrl = "~/Admin/AdminInventory.aspx"
        End If
    End Sub

    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        sdsAdmin.DataBind()
    End Sub

This basically binds the label in the Datalist. I know it binds the data to the correct value because I have tested it with a "Test Button" that when you click it it Databinds the label and it displays the desired result.

My problem is that I can't seem to get the code to find the Label within the Datalist "ItemTemplate"

Here is the code of the DataList:

Code:
                <asp:DataList ID="dlAdmin" Runat="server" DataSourceID="sdsAdmin">

                    <ItemTemplate>
                        <asp:Label Text='<%# Eval("Admin") %>' Runat="server" ID="AdminLabel"> </asp:Label>
                    </ItemTemplate>

                </asp:DataList>

Like I said I know the label updates to the appropriate stuff. I just am having trouble getting the Text from the Label after it has been bound.

Any ideas????
 
When I run the script for the page it returns a "Null" value error

Code:
Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 5:          sdsAdmin.DataBind()
Line 6:          Dim AdminLblRef As Label = CType(dlAdmin.FindControl("AdminLabel"), Label)
Line 7:          Dim AdminLblText As String = AdminLblRef.Text
Line 8:          
Line 9:
 
You'll have to step through the controls hierarchy for your DataList control to find out exactly where the label is within the controls collection. Once you've found that out you can use the relevant FindControl method of the parent.


Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
I tried and Tried with the FindControl function. Never could get it to find it.

I ended up writing a SQL Query since it would only return one result each time. Ended up using "ExecuteScalar" here is the code I ended up using

Code:
    Dim DataSource As SqlConnection
    Protected Sub Login_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ConString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|ASPNETDB.MDF;user instance=True;Integrated Security=True;Initial Catalog=ASPNETDB;"
        Dim MyCommand As SqlCommand
        Dim UserNameString As String = User.Identity.Name
        Dim SelectCommand As String = "SELECT [Admin] FROM [UserInfo] WHERE ([UserName] = @UserName)"
        Dim AdminStatus As Int32 = 0
        
        DataSource = New SqlConnection(ConString)
        DataSource.Open()
        MyCommand = New SqlCommand(SelectCommand, DataSource)
        MyCommand.Parameters.Add(New SqlParameter("@UserName", SqlDbType.NVarChar, 50))
        MyCommand.Parameters("@UserName").Value = UserNameString
        AdminStatus = Convert.ToInt32(MyCommand.ExecuteScalar())
        MyCommand.ExecuteNonQuery()
        MyCommand.Connection.Close()
        
        If AdminStatus = "0" Then
            Response.Redirect("~/Users/UserInventory.aspx")
        ElseIf AdminStatus = "1" Then
            Response.Redirect("~/Admin/AdminInventory.aspx")
        End If
    End Sub

It runs a query and uses the ExecuteScalar command.

Then I have it redirecting to the appropriate places via the AdminStatus returned value.

I didn't use roles in my instance because there is minimal differentiation in the roles.
 
You did exactly what I was thinking. Nice job, although would change it to use a stored procedure instead of inline SQL.
 
What do you mean by stored procedure? Can you give me an example of that in my situation?
 
Since you are using SQL Express, which is a chopped down version of SQL Server, you can use stored procedures. A stored procedure is code that lives on the SQL Server. You can look it up in help or the many on-line references.
 
jbensons001 I have a new problem.

I changed the password type to "Encrypted" now the code above doesn't work. It returns "0" every time.

Any ideas?
 
I believe that it is because my user is not authenticated yet via the password so the User.Identity.Name is not stored yet.

Any ideas?
 
Well as they say in the south "If it was a snake it woulda bit me!"

I ended up using:

Code:
Dim UserNameString as String = Login.UserName

instead of

Code:
Dim UserNameString as String = User.Identity.Name
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top