M
MikeY
Hi Eveyone,
I'm trying to sort my ArrayList, but I'm getting stumped. Hopefully someone
can help me out. I'm using C# Windows forms, but Console app for testing.
What I am doing is pulling my data from my database and putting the data
into an ArrayList. from there I want to be able to sort the data out. This
is where I"m getting stumped. Getting at, and viewing the data is all good
until I try to use "myArrayList.Sort". Trying various sort methods pull
errors ie: "Specified IComparer threw an exception". As for me data I won't
know the amount of data that there will be in the database, just that I want
to sort the first row of my arraylist. A sample of my code follows. Txs in
advance and Any and all help is appreciated.
using System;
using System.Collections;
using System.Data.OleDb;
namespace Ch11Ex01_ArrayList
{
class Class1
{
static void Main(string[] args)
{
try
{ //Connetion to DataBase
OleDbConnection myConnection = new OleDbConnection(@"Provider =
Microsoft.Jet.OLEDB.4.0;Data Source = C:\Host.mdb");
//Open Connection to DataBase
myConnection.Open();
//SQL Command for myConnection
OleDbCommand myCommand = myConnection.CreateCommand();
//Initialize SQL SELECT command to retrieve data
myCommand.CommandText = "SELECT * FROM Food_Lunch_Menu";
//Create DataReader
OleDbDataReader myDataReader = myCommand.ExecuteReader();
ArrayList myArrayList = new ArrayList();
while(myDataReader.Read())
{
myName myItem = new myName(myDataReader["Index"].ToString());
myItem.myIndex(myDataReader["Name"].ToString());
myArrayList.Add(myItem);
Console.WriteLine(myItem.Name + " " + myItem.Index);
}
myArrayList.Sort(0, 0, null); <-Problem Spot
foreach(myClass myItems in myArrayList)
{
Console.WriteLine(myItems.Name + " " + myItems.Index);
}
myDataReader.Close();
myConnection.Close();
}
catch(Exception myException)
{
Console.WriteLine(myException.Message);
}
}
}
}
I'm trying to sort my ArrayList, but I'm getting stumped. Hopefully someone
can help me out. I'm using C# Windows forms, but Console app for testing.
What I am doing is pulling my data from my database and putting the data
into an ArrayList. from there I want to be able to sort the data out. This
is where I"m getting stumped. Getting at, and viewing the data is all good
until I try to use "myArrayList.Sort". Trying various sort methods pull
errors ie: "Specified IComparer threw an exception". As for me data I won't
know the amount of data that there will be in the database, just that I want
to sort the first row of my arraylist. A sample of my code follows. Txs in
advance and Any and all help is appreciated.
using System;
using System.Collections;
using System.Data.OleDb;
namespace Ch11Ex01_ArrayList
{
class Class1
{
static void Main(string[] args)
{
try
{ //Connetion to DataBase
OleDbConnection myConnection = new OleDbConnection(@"Provider =
Microsoft.Jet.OLEDB.4.0;Data Source = C:\Host.mdb");
//Open Connection to DataBase
myConnection.Open();
//SQL Command for myConnection
OleDbCommand myCommand = myConnection.CreateCommand();
//Initialize SQL SELECT command to retrieve data
myCommand.CommandText = "SELECT * FROM Food_Lunch_Menu";
//Create DataReader
OleDbDataReader myDataReader = myCommand.ExecuteReader();
ArrayList myArrayList = new ArrayList();
while(myDataReader.Read())
{
myName myItem = new myName(myDataReader["Index"].ToString());
myItem.myIndex(myDataReader["Name"].ToString());
myArrayList.Add(myItem);
Console.WriteLine(myItem.Name + " " + myItem.Index);
}
myArrayList.Sort(0, 0, null); <-Problem Spot
foreach(myClass myItems in myArrayList)
{
Console.WriteLine(myItems.Name + " " + myItems.Index);
}
myDataReader.Close();
myConnection.Close();
}
catch(Exception myException)
{
Console.WriteLine(myException.Message);
}
}
}
}