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()
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
_sqlConn = New SqlConnection(_connString)
_sqlConn.Open()
Dim sqlCmd As New SqlCommand("select * from [Users]", _sqlConn)
Dim dr As SqlDataReader = sqlCmd.ExecuteReader()
Dim userColl As New UserCollection
Dim user As User
While dr.Read()
user = New User
user.UserName = dr("UserName").ToString
user.Password = dr("Password").ToString
user.FirstName = dr("FirstName").ToString
user.Surname = dr("Surname").ToString
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