Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Is it possible to add the result of a Query to a collection

Like

SQL
SELECT LastName,FirstName,MiddleName,Country FROM Contact ORDER BY LastName


VB
Dim Contact As New List(Of String)


My purpose it to Search my Collection So that I Don't Need to Query Again and Again

Hope You can help me with This :)


OK I can't do it right,

I Created a Class "CollectContact" which Have Public Property for LastName, FirstName, and FamilyName. And a Class "ListContact" which contains
VB
Public Class CollectContact
Private _FirstName As String = String.Empty
Friend Property FirstName() As String
       Get
           Return _FirstName
       End Get
       Set(ByVal value As String)
           _FirstName= value
       End Set
   End Property
Private _LastName As String = String.Empty
Friend Property LastName () As String
       Get
           Return _LastName 
       End Get
       Set(ByVal value As String)
           _LastName = value
       End Set
   End Property
End Class


VB
Public Class ListContact
Public Sub New()
        _List = New List(Of CollectContact)
End Sub
Private _List As List(Of CollectContact)
Public Property ListOfFriend() As List(Of CollectContact)
        Get
            Return _Profile
        End Get
        Set(ByVal value As List(Of CollectContact))
            _Profile = value
        End Set
    End Property
End Class


VB
Dim Contact As New CollectContact
Dim ContactList As New ListContact

Now on my DataReader.Read
VB
Contact.FirstName = DataReader("First Name").ToString
Contact.LastName = DataReader("Last Name").ToString
ContactList.ListOfFriend.Add(Contact)

Lets say I have a Data:
FirstName LastName
Ron McMiller
James Baker
Sarah Jasmine

Now When I Debug the Return Value of ListOfFriend

I get 3 Rows that have same FirstName & LastName
Number of Rows are correct but the FirstName & LastName are all the same
Posted
Updated 21-Jun-13 14:29pm
v3
Comments
Sergey Alexandrovich Kryukov 20-Jul-13 0:55am    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership. And the fact you even self-accepted some formally is just outrageous, a sure way for a ban. I hope you won't do it after this warning.

Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

Yes Its Possible.
The Steps you have to follow is
1. Create an Object class which should contains the fields to fetch your data from sql, so the type of both fields in sql table and object class should be same.
ex:
public Class BOEmployee
{
public string LastName{get;set;}
public string FirstName{get;set;}
.........
........
}
2. Create an object/instance of the class where you are query is passing to database
And Declare your collection of your type, here i am using ObservableColletion of your object class type
ex:
BOEmployee boemployee;
ObservableCollection CLemployee== new ObservableCollection<boemployee>();
3. Excecute the query with your connection
ex:
SqlConnection Conn = AppConfiguration.Connection;
SqlDataReader rdr = SqlHelper.ExecuteReader(Conn, CommandType.Text,
"SELECT LastName,FirstName,MiddleName,Country FROM Contact ORDER BY LastName");
4. Read the result from DataReader and add the results to the collection
While (rdr.Read())
{
boemployee=new BOEmployee();
boemployee.FirstName=rdr["FirstName"].ToString();
..................
...................
CLemployee.Add(boemployee);
}
Now you have all the data in the collection , you can filter it according to your usage No need to connect with database always....
 
Share this answer
 
Comments
Zoltán Zörgő 21-Jun-13 2:09am    
The idea is good, although you don't need observable collection for that. A simple generic list would do that.
iMaker.ph 21-Jun-13 2:38am    
Fill free to share your own solution :)
iMaker.ph 21-Jun-13 5:08am    
By the way, How can I search on my Collection

Here's mine
Public AccountProfile As New List(Of Object)

If I want to search LastName and FirstName and Get the Entire Row or Specific cell like PhoneNumber

Thanks in advance :)
What I did wrong is Declaring the
VB
Dim Contact As New CollectContact
outside my Reader so stupid of me X(

Another Question how Can I search a Name on my ListContact ?

I tried
XML
Public Sub SearchFirstName(ByVal Name As String)
       Dim _ListContact As New ListContact
       Dim cAccount As CollectContact = _ListContact.ListOfFriend.Find(AddressOf sName)
       If Not cAccount Is Nothing Then
           'How to getting the FirstName, LastName, And MiddleName
       End If
   End Sub

   Private Function sName(ByVal cName As AllUsers) As Boolean
       If cName.UserName = Name Then
           Return True
       Else
           Return Nothing
       End If
   End Function
 
Share this answer
 
v2

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