Trying to Start Simple

Trying to Start Simple

Old forum URL: forums.lhotka.net/forums/t/2999.aspx


SimpleCSLA posted on Thursday, June 07, 2007

It took over two weeks, but I've finally gotten remoting to work and gotten Project Tracker to work - during much ridicule of my fellow programmers at my job.  To get started in anything I personally need to start simple and work my way up to the more complicated.

So now I'm reading carefully through the book to write a very simple class that will get data through a StoredProcedure.  However, every example in the book seems to rely on another object (projectList.vb relies on Project.vb / RoleList relies on Role).  I can't seem to find an example of one class returning a value that doesn't rely on another class.

Here's a recommendation for Rocky's next book: Put in a "Getting Started" chapter, which ignores the MasterWebPage and Themes, and walks the user through creating three simple classes (one that reads values from a table, one that updates that data and one that inserts data).  Then write one ASPX page that uses each of those three classes.  Once we're done with the "Getting Started" chapter, lead us into the child objects.

I would GREATLY appreciate it if someone could tell me where my code is wrong below (wrong because i can't seem to call it from an aspx page and returned the values).

Also, if anyone has some simple classes written in VB they'd like to share, that would be very helpful - or even tell me a page # of the book.  On on page 542 (no I didn't fully read the first 541 pages, most of it is WAYYY over my head) and while Project Tracker works perfectly, the CSLA Data Source tool is not showing in my Toolbox (so that would also be great if someone could tell me how to get that to show up). 

<Serializable()> _
    Public Class Test
    Inherits Csla.NameValueListBase(Of Integer, String)

#Region " Business Methods "

    Public Shared Function UserList() As Integer

        Dim list As Test = GetList()
        If list.Count > 0 Then
            Return list.Items(0).Key
        End If

    End Function

#End Region

#Region " Factory Methods "

    Private Shared mList As Test

    Public Shared Function GetList() As Test

        If mList Is Nothing Then
            mList = DataPortal.Fetch(Of Test) _
              (New Criteria(GetType(Test)))
        End If
        Return mList

    End Function

    Public Shared Sub InvalidateCache()

        mList = Nothing

    End Sub

    Private Sub New()
        ' require use of factory methods
    End Sub

#End Region
#Region " Data Access "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)

        Me.RaiseListChangedEvents = False
        Using cn As New SqlConnection(Database.TestConnection)
            cn.Open()
            Using cm As SqlCommand = cn.CreateCommand
                cm.CommandType = CommandType.StoredProcedure
                cm.CommandText = "getUserList"

                Using dr As New SafeDataReader(cm.ExecuteReader)
                    IsReadOnly = False
                    With dr
                        While .Read()
                            Me.Add(New NameValuePair( _
                              .GetInt32("UserID"), .GetString("UserName")))
                        End While
                    End With
                    IsReadOnly = True
                End Using
            End Using
        End Using
        Me.RaiseListChangedEvents = True

    End Sub

#End Region

End Class

ajj3085 replied on Thursday, June 07, 2007

A couple of things.

First, you have overloads instead of overrides for your DataPortal_Fetch.  This is why things aren't working as you expect.  Its a shame VB.Net makes you use a keyword to overload a function, especially with the overrides keyword..

Second, you can simplify the DP.F call to this:

mList = DataPortal.Fetch(Of Test) ()

If you simpifly like above, you would declare the DataPortal_Fetch as such:

Private Sub DataPortal_Fetch()

End Sub

HTH

Andy

mr_lasseter replied on Thursday, June 07, 2007

Every example in the book does not depend on another object.  The ProjectList depends on the ProjectInfo class. The Project Class does not depend on any other classes, except for the Project Resources class, which you don't have to implement if you want one simple class.  You should be able to use it as an example of how to get it to work.  As for your code below are you getting any errors or is it just not loading the data?  Have you stepped through the code using the debugger to see what is going on?

To get the CslaDataSource in the toolbox do the following:

Right click in the toolbox and select Choose Items.
When the Choose toolbox Items window appears click browse.  Find the Csla.dll on your machine and select it.
Click ok and the items will be added to the toolbox. 

Copyright (c) Marimer LLC