Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello guys, got a problem here. As you see I am binding using collection on app code. if my code is like this one
_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
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
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
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 28-Sep-11 22:26pm
janwel1.9K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Afzaal Ahmad Zeeshan 82
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 29 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100