Personalization - Object reference not set to an instance of an object

Last post 10-25-2005 7:15 PM by rcurtis. 1 replies.

Sort Posts:

  • Personalization - Object reference not set to an instance of an object

    10-23-2005, 3:12 PM
    • Member
      321 point Member
    • rcurtis
    • Member since 07-14-2002, 4:03 PM
    • Posts 78
    I am trying to get a Custom personalization provider to work with my existing SQL table. When I try and call <%= Profile.CompanyName %> in the page I get

    Object reference not set to an instance of an object.
    Line 40:     Public Overridable Property CompanyName() As String
    Line 41: Get
    Line 42: Return CType(Me.GetPropertyValue("CompanyName"),String)
    Line 43: End Get
    Line 44: Set
    What do I need to do to retrieve Profile values from my table?

    Here's the GetPropertyValues from the class

    Public Overrides Function GetPropertyValues(ByVal context As System.Configuration.SettingsContext, ByVal ppc As System.Configuration.SettingsPropertyCollection) As System.Configuration.SettingsPropertyValueCollection

                Dim connString As String
                connString = "Server=xxx;User ID=xxx;password=xxx;Database=xxx"
                Dim commString As String
                commString = "SELECT CompanyID, CompanyName, Email from Company WHERE Userid = 123123123"
                Dim conn As SqlConnection = New SqlConnection(connString)
                Dim cmd As SqlCommand = New SqlCommand(commString, conn)

                ' Open the connection and execute the reader
                conn.Open()
                Dim reader As SqlDataReader
                reader = cmd.ExecuteReader()

                While reader.Read()
                    ppc("CompanyID").DefaultValue = reader("CompanyID")
                    ppc("CompanyName").DefaultValue = reader("CompanyName")
                    ppc("Email").DefaultValue = reader("Email")
                End While

                ' Close connections
                reader.Close()
                conn.Close()
                cmd = Nothing

            End Function

  • Re: Personalization - Object reference not set to an instance of an object

    10-25-2005, 7:15 PM
    • Member
      321 point Member
    • rcurtis
    • Member since 07-14-2002, 4:03 PM
    • Posts 78
    I was able to solve my own problem. The code I used about is from the book ASP.NET 2.0 the beta version and I couldn't get it to work as it is written up in the book. I used code based on another posting in this form which works fine. It works if the CompanyID is hardcoded into the SQL statement. What I need to do next is to capture the CompanyID from the Logged in member and pass it to the GetPropertyValues function. Any clues?


     Public Overrides Function GetPropertyValues(ByVal context As System.Configuration.SettingsContext, ByVal ppc As System.Configuration.SettingsPropertyCollection) As System.Configuration.SettingsPropertyValueCollection

                Dim svc As SettingsPropertyValueCollection = New SettingsPropertyValueCollection()

                Dim connString As String
                'Dim Username As String
                'Dim password As String
                'Dim CompanyID As Integer
                'Dim CompanyName As String
                'Dim Email As String
                connString = "Server=xxxx;User ID=xxx;password=xxx;Database=xxxx"
                Dim commString As String
                commString = "SELECT CompanyID, CompanyName, Email from Company WHERE CompanyID = 1"
                Dim conn As SqlConnection = New SqlConnection(connString)
                Dim cmd As SqlCommand = New SqlCommand(commString, conn)
                'cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = "123123123"
                'cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = password

                ' Open the connection and execute the reader
                conn.Open()
                Dim reader As SqlDataReader
                reader = cmd.ExecuteReader()

                While reader.Read()

                    For Each prop As SettingsProperty In ppc
                        Dim pv As SettingsPropertyValue = New SettingsPropertyValue(prop)

                        Select Case prop.Name
                            Case "CompanyID"
                                pv.PropertyValue = reader("CompanyID")
                            Case "CompanyName"
                                pv.PropertyValue = reader("CompanyName")
                            Case "Email"
                                pv.PropertyValue = reader("Email")
                        End Select
                        svc.Add(pv)
                    Next

                End While

                Return svc

                ' Close connections
                reader.Close()
                conn.Close()
                cmd = Nothing

            End Function

Page 1 of 1 (2 items)