Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2006, 02:37 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default System.NullReferenceException: Object reference no

Hi,

I am having problems in trying to resolve a part of code of which I am creating a cookie, but keep getting the above error message on a particular line of code: -

Sub Page_Load(Sender As Object, E As EventArgs)

    If(Request.IsAuthenticated = True) Then
        pnlEdit.Visible = true

        lblStatus.text = "You are logged in as: " _
            & Request.Cookies("UserNameCookie").Value

                If Request.Cookies("UserLevelCookie").Value = "admin" and _
                    right$(Request.ServerVariables("APPL_PHYSICAL_PATH "),11)="KintonWebsite" Then
                    lblStatus.Text &= "<br><a href='customeradmin.aspx'>Customer Admin Page</a><br>"
                End If
    End If

End Sub

Sub btn_Logout(Sender As Object, e As EventArgs)

    FormsAuthentication.Signout
    pnlEdit.Visible = False

        If right$(Request.ServerVariables("PATH_INFO"), 10)="admin.aspx" Then
            Response.Redirect("default.aspx")
        End If

End Sub

The line in question is the one in red, any suggestions or tips would be greatly appreciated.

Thanks
Sharon
 
Old October 24th, 2006, 02:44 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Your cookies object doesn't exist, at least not in the context you are trying to access it. I threw this together real quick just to test it:

        Response.Cookies("userNameCookie").Value = "Doug"
        Response.Cookies("userNameCookie").Expires = DateTime.Now.AddDays(1)

        Response.Write(Request.Cookies("userNameCookie").V alue)

And this writes out 'Doug' to the browser. How are you populating your cookies?

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 24th, 2006, 02:49 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thanks for your reply, I am trying to access the cookies via a database connection which is created on the login page, please see the below code: -

Sub btnLogin_Click(sender As Object, e As EventArgs)

    Dim CustomersDB as System.Data.iDataReader
    CustomersDB = Customers()
    While CustomersDB.Read

        Dim CustomerLogin as String = CustomersDB("SiteLogin")
        Dim CustomerPassword as String = CustomersDB("SitePassword")
        Dim AdminLevel As String = CustomersDB("AdminLevel")

        If (txtUserName.Text = CustomerLogin And txtPwd.Text = CustomerPassword) Then

            Dim UserNameCookie as New HttpCookie("UserNameCookie")
            UserNameCookie.Value = txtUserName.Text
            Response.Cookies.Add(UserNameCookie)

            Dim UserLevelCookie as New HttpCookie("UserLevelCookie")
            UserLevelCookie.Value = AdminLevel
            Response.Cookies.Add(UserLevelCookie)

            FormsAuthentication.RedirectFromLoginPage(txtUserN ame.Text, true)
        Else
            lblMsg.Text = "Invalid Username or Password!"
        End If

    End While

    CustomersDB.Close()

End Sub

Function Customers() As System.Data.IDataReader

    Dim connectionString As String = _
        ConfigurationSettings.AppSettings("ConnectionStrin g")

    Dim dbConnection As System.Data.IDbConnection = _
        New System.Data.OleDb.OleDbConnection(connectionString )

    Dim queryString As String = "SELECT [Customers].[SiteLogin]," & _
        "[Customers].[SitePassword], [Customers].[AdminLevel] FROM [Customers]"

    Dim dbCommand As System.Data.IDbCommand = _
        New System.Data.OleDb.OleDbCommand

        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.Open
    Dim dataReader As System.Data.IDataReader = _
        dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

        Return dataReader

End Function

I have also checked my web.config but nothing to indicate that there is a broken file path.

Even a hint of the direction I need to look into would be of some help as I have pondering on this for a few days now.

It's probably something really simple that I have missed.

Thanks
Sharon
 
Old October 24th, 2006, 02:56 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Instead of redirecting to the LoginPage, do:

Response.Write(Request.Cookies("userNameCookie").V alue)

See if a value is wrote out there or if you get the same error, that will at least tell you if the cookie is not being created at all or if it is being lost somewhere in transition.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 24th, 2006, 03:08 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I am getting no value so don't believe the cookie is being created as I am now getting an error of Expression does not produce a value.

I will check my coding on the login page again.

Thanks

Sharon
 
Old October 24th, 2006, 03:35 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No problem, post again if you are still having problems =]

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 24th, 2006, 04:03 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Still having problems with this and keep getting the new error of Expression does not produce a value.

I have coded the below on the Login page to create a cookie but doesn't appear to be creating this: -

Sub btnLogin_Click(sender As Object, e As EventArgs)

    Dim CustomersDB as System.Data.iDataReader
    CustomersDB = Customers()
    While CustomersDB.Read

        Dim CustomerLogin as String = CustomersDB("SiteLogin")
        Dim CustomerPassword as String = CustomersDB("SitePassword")

        If (UserName.Text = CustomerLogin And txtPwd.Text = CustomerPassword) Then


            Dim UserNameCookie as New HttpCookie("UserNameCookie")
            UserNameCookie.Value = UserName.Text
            Response.Cookies.Add(UserNameCookie)

            FormsAuthentication.RedirectFromLoginPage(txtUserN ame.Text, true)
        Else
            lblMsg.Text = "Invalid Username or Password!"
        End If

    End While

    CustomersDB.Close()

End Sub

Function Customers() As System.Data.IDataReader

    Dim connectionString As String = _
        ConfigurationSettings.AppSettings("ConnectionStrin g")

    Dim dbConnection As System.Data.IDbConnection = _
        New System.Data.OleDb.OleDbConnection(connectionString )

    Dim queryString As String = "SELECT [Customers].[SiteLogin]," & _
        "[Customers].[SitePassword] FROM [Customers]"

    Dim dbCommand As System.Data.IDbCommand = _
        New System.Data.OleDb.OleDbCommand

        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.Open
    Dim dataReader As System.Data.IDataReader = _
        dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

        Return dataReader

End Function

Anymore suggestions?

Thanks
Sharon
 
Old October 24th, 2006, 04:11 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First here are some articles about FormsAuthentication:
http://www.ondotnet.com/pub/a/dotnet...rmsauthp1.html
http://www.4guysfromrolla.com/webtech/110701-1.shtml

And this is straight off the MSDN
Dim aCookie As New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)

http://msdn.microsoft.com/library/de...Cookies101.asp

Also, at what line are you getting the expression error?

IMHO, I am not seeing what your problem could be here...its very strange.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 24th, 2006, 04:15 PM
Authorized User
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thanks for the link, I will add it to my favourites.

The line in question is pointing again at my header.ascx page: -

Sub Page_Load(Sender As Object, E As EventArgs)

    If(Request.IsAuthenticated = True) Then
        pnlEdit.Visible = true

        lblStatus.Text = "You are logged in as: " _
            & Response.Write(Request.Cookies("userNameCookie").V alue)
    End If

End Sub

Sub btn_Logout(Sender As Object, e As EventArgs)

    FormsAuthentication.Signout
    pnlEdit.Visible = False
            Response.Redirect("default.aspx")

End Sub


Line with the problem is the lblStatus line again.

I can't see anything that is wrong with this.

Sharon
 
Old October 24th, 2006, 04:23 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

If memory serves me, cookies are case sensitive, change 'user' to 'User'

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature





Similar Threads
Thread Thread Starter Forum Replies Last Post
System.NullReferenceException JayLou BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 April 6th, 2007 12:55 PM
System.NullReferenceException lwheless BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 24th, 2006 05:47 AM
System.NullReferenceException: Object reference no nidy_online VS.NET 2002/2003 2 June 9th, 2004 09:17 PM
System.NullReferenceException: Object reference... nordestgaard All Other Wrox Books 4 January 7th, 2004 01:06 AM
[NullReferenceException: Object reference not set creiche ASP.NET 1.0 and 1.1 Basics 3 September 23rd, 2003 08:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.