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()
 
<pre>
   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()
 
<pre>
     Dim sqlCmd As New SqlCommand(&quot;select * from [Users]&quot;, _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(&quot;UserName&quot;).ToString
         user.Password = dr(&quot;Password&quot;).ToString
         user.FirstName = dr(&quot;FirstName&quot;).ToString
         user.Surname = dr(&quot;Surname&quot;).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
 
<pre>
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</pre>
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 455
1 Sergey Alexandrovich Kryukov 445
2 Maciej Los 290
3 ProgramFOX 275
4 CHill60 225
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 29 Sep 2011
Copyright © CodeProject, 1999-2015
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