Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys, got a problem here. As you see I am binding using collection on app code. if my code is like this one
VB
_userDAL = New UserDAL
Dim users As UserCollection = _userDAL.SelectAll()

'Dim dr As DataTable = getTable()
gvUserList.DataSource = users
gvUserList.DataBind()

everything works fine but it doesnt have paging. Now to have a paging I need to bind it on datatable so this is my code now
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       bind()
   End Sub
   Private Sub bind()
       _userDAL = New UserDAL
       Dim users As UserCollection = _userDAL.SelectAll()

       Dim dr As DataTable = getTable()
       gvUserList.DataSource = dr
       gvUserList.DataBind()

   End Sub
   Private Function getTable() As DataTable
       _userDAL = New UserDAL
       Dim users As UserCollection = _userDAL.SelectAll()
       Dim adap As New SqlDataAdapter(users)
       Dim dt As New DataTable()
       adap.Fill(dt)
       Return dt
   End Function

but im getting an error saying 'collection' cannot be converted to sqlcommand pointing at users. I know that my logic is wrong but i cant find a suitable tutorial regarding on how to bind gridview on regards to collection. Could you help me?

UserDAL.vb
VB
Public Function SelectAll() As UserCollection
     Try
         'select * from User
         _sqlConn = New SqlConnection(_connString)
         _sqlConn.Open()

         Dim sqlCmd As New SqlCommand("select * from [Users]", _sqlConn)

         Dim dr As SqlDataReader = sqlCmd.ExecuteReader()

         'Create user collection
         Dim userColl As New UserCollection
         Dim user As User

         While dr.Read()
             'Create User object
             user = New User
             user.UserName = dr("UserName").ToString
             user.Password = dr("Password").ToString
             user.FirstName = dr("FirstName").ToString
             user.Surname = dr("Surname").ToString

             'add newley created user to collection
             userColl.Add(user)
         End While
         dr.Close()

         Return userColl

     Finally
         If _sqlConn IsNot Nothing Then
             If _sqlConn.State = Data.ConnectionState.Open Then
                 _sqlConn.Close()
             End If
         End If
     End Try
 End Function

UserCollection.vb
VB
Public Class UserCollection
    Inherits CollectionBase

    Public Sub Add(ByVal item As User)
        List.Add(item)
    End Sub

    Public Sub Remove(ByVal item As User)
        List.Remove(item)
    End Sub

    Default Public Property Item(ByVal index As Integer) As User
        Get
            Return DirectCast(List(index), User)
        End Get
        Set(ByVal value As User)
            List(index) = value
        End Set
    End Property
Posted

1 solution

Hi,

In your DAL I thing you should do it this way to return the
List of User

Public Shared Function SelectAll() As List(Of User)
       'Create List Of User
       Dim userColl As New List(Of User)
       Try
           'select * from User
           _sqlConn = New SqlConnection(_connString)
           _sqlConn.Open()
           Dim sqlCmd As New SqlCommand("select * from [Users]", _sqlConn)
           Dim dr As SqlDataReader = sqlCmd.ExecuteReader()
           While dr.Read()
               'Create User object
               Dim userRec As User = New User
               userRec.UserName = dr("UserName").ToString
               userRec.Password = dr("Password").ToString
               userRec.FirstName = dr("FirstName").ToString
               userRec.Surname = dr("Surname").ToString
               'add newly created userRec to List Of User
               userColl.Add(userRec)
           End While
           dr.Close()
       Catch
           'add something here to catch exption message ...?
       End Try
       Finally
           If _sqlConn IsNot Nothing Then
               If _sqlConn.State = Data.ConnectionState.Open Then
                   _sqlConn.Close()
               End If
           End If
       End Try
       Return userColl
   End Function


Regards,

Algem.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900