Load class object into a collection

Hi all

I have the class below..
I created an instance of the Class as well as the instance of the Collection.
How can I populate the Collection with all the instance of the class.
At the moment only 1 object is loaded into the collection..
Any help

SchemaType typeOfSchema = new SchemaType();
        typeOfSchema.LoadAll();
        //Create an instanc of schema Type Collection
        SchemaTypesCollection typeOfSchemaCollection = new SchemaTypesCollection();
        typeOfSchemaCollection.Add(typeOfSchema);

Open in new window


Snipet 1
public SchemaType(Int32 ID, String Description, DateTime CreateDate)
		{
			this.ID = ID;
			this.Description = Description;
			this.CreateDate = CreateDate;
		}

Open in new window


Snipet 2
public class SchemaTypesCollection : CollectionBase, IEnumerable, IEnumerator
    {

        private int index = -1;

        public SchemaTypesCollection()
        {
            this.index = -1;
        }

        public void Add(SchemaType schemaType)
        {
            /*
             *  Example for applying business Logic
             * 
             */

            if (schemaType != null)
            {
                throw new Exception("Your passed SchemaType Object is null");
            }

            /*
             * Normal behaviour of a Collection
             */

            this.List.Add(schemaType);
        }
        public void Remove(SchemaType schemaType)
        {
            this.List.Remove(schemaType);
        }

        public SchemaType this[int index]
        {
            get { return (SchemaType)this.List[index]; }
            set { this.List[index] = value; }
        }

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator Members

        public Object Current
        {
            get
            {
                return this.List[index];
            }
        }

        public bool MoveNext()
        {
            this.index++;
            return (this.index < this.List.Count);
        }

        public void Reset()
        {
            this.index = -1;
        }

        #endregion

        public void Save(SchemaTypesCollection collection)
        {
            try
            {

                /*
                 *  Insert all items of 
                 */
            }
            catch (Exception e)
            {
                /*
                 *  if you get an error in between Rollback
                 */
                throw e;
            }
            finally
            {
            }
        }

Open in new window

ZURINETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
"How can I populate the Collection with all the instance of the class."

I don't see more than one instance of a class.  

You could create an AddRange method:

public void AddRange(IEnumerable<SchemaType> range)
{
    this.List.AddRange(range);
}

Open in new window

0
ZURINETAuthor Commented:
The schemaType data is comng from a DB
And it has up to 10 rows

public SchemaType(Int32 ID, String Description, DateTime CreateDate)
		{
			this.ID = ID;
			this.Description = Description;
			this.CreateDate = CreateDate;
		} 

Open in new window

0
Bob LearnedCommented:
Then, you should be able to use AddRange, if the internal List supports that.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ZURINETAuthor Commented:
I dont have AddRAnge, I only have GetEnumerator
0
Bob LearnedCommented:
Are you saying that you can't add the AddRange method that I showed above to your code?
0
ZURINETAuthor Commented:
I can add the rage method..
But : the AddRange... is not implemented in the class

public void AddRange(IEnumerable<SchemaType> range)
{
    this.List.AddRange(range);
}
0
Bob LearnedCommented:
Right, researched CollectionBase, and found that it exposes the List property as IList, that doesn't support AddRange.

Is there a reason that you need inheritance and CollectionBase, rather than List<SchemaType>?
0
ZURINETAuthor Commented:
The problem is that I have up to 20 Classes, where this method has been implemented..
need to update lot of classes... looking for a Short if there is any..

How can I use LIST<SchemaType> to solve this problem
0
Bob LearnedCommented:
List<T> has all the implementations of IList, plus the AddRange method.  Creating your own custom collection class is a very specific requirement, so I was wondering if you had one.

Can you show me the LoadAll method from this line?

 typeOfSchema.LoadAll()
0
ZURINETAuthor Commented:
public virtual void LoadAll() 
        {
            SqlConnection sqlconn = Database.GetConn();           
            SqlCommand sqlcmd = new SqlCommand("GetAll", sqlconn);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            try
            {               
                sqlconn.Open();             
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    sqldreader.Read();                    
                    this.ID = (int) sqldreader["ID"]; 
                    this.Description = (string) sqldreader["Desc"];
                    this.CreateDate = (DateTime)sqldreader["CrDate"];
                }
                else
                {
                    throw new Exception("There is no Schema avaialbe!");
                }              
                sqldreader.Close();
				}
            catch (SqlException ex)
            {
                StringBuilder errorMessages = new StringBuilder();                
            }
            finally
            {              
                sqlconn.Close();
            }       
        
        }

Open in new window

0
Bob LearnedCommented:
LoadAll is only calling the SqlDataReader.Read method once, which means that you only get a single value.

Here is a suggested change showing some C# concepts:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Demo
{
    public class DataReader
    {

        private readonly string _connectionString;

        public DataReader(string connectionString)
        {
            _connectionString = connectionString;
        }

        public virtual List<SchemaTypeRecord> LoadAllSchemaTypes()
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("GetAll", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    using (var reader = command.ExecuteReader())
                    {
                        var dataTable = new DataTable();
                        dataTable.Load(reader);

                        var rowList = dataTable.AsEnumerable();

                        var schemaList = rowList
                            .Select(x => new SchemaTypeRecord
                                {
                                    Id = x.Field<int>("ID"),
                                    Description = x.Field<string>("Desc"),
                                    CreateDate = x.Field<DateTime>("CrDate")
                                })
                            .ToList();

                        return schemaList;
                    }
                }
            }

        }
    }

    public class SchemaTypeRecord
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public DateTime CreateDate { get; set; }
    }

}

    

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZURINETAuthor Commented:
Hoi TheLearnedOnePosted

Thanks for the post.. Will have a look at it ..
Load all is the problem

Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.