stpatrck 15 Newbie Poster

If you are not restricted to using an array you may want to consider List<Student>. This would simplify your code to be something like...

Initialize the list of students

var ClassList = new List<Student>();

Modify your add click to...

private void btnAdd_Click(object sender, EventArgs e)
        {
            // Create new student and assign name etc provided by user
            Student _Student = new Student();
            _Student.Name = txtName.Text;
            _Student.Surname = txtSurname.Text;
            _Student.Age = Convert.ToInt32(txtAge.Text);
            _Student.ITMark = Convert.ToInt32(txtIT.Text);
            _Student.MathMark = Convert.ToInt32(txtEng.Text);
            _Student.EngMark = Convert.ToInt32(txtMath.Text);

            // ... omitted for brevity

            ClassList.Add(_Student);

            // .. clear input controls.
        }

Then no changes are needed to your display method

private void displayAll_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            for (int j = 0; j < ClassList.Count(); j++)
            {
                richTextBox1.Text += ("Student: " + Convert.ToString(j) +
                                     "\nName: " + ClassList[j].Name +
                                     "\nSurname: " + ClassList[j].Surname +
                                     "\nAge: " + ClassList[j].Age +
                                     "\nStudent Average: " + Convert.ToString(ClassList[j].AverageMark) +
                                     "\n" + "\n");
            }
            //MessageBox.Show("Display all students.");
        }
ddanbe commented: for overall effort :) +15
stpatrck 15 Newbie Poster

On a side note, I prefer to use the Length property over the Count() extension method when dealing with arrays.

A good thread about array.length vs array.count()

stpatrck 15 Newbie Poster

If you are initializing an array with the size of 50 ClassList.Count() will always be fifty no matter how many Students you add to the list. This means that your 'for' loop is looking at null positions. Here are a couple of suggestions to try.

First, since you already have a 'counter' for the number of students that have been added you can use it in place of the 'ClassList.Count()' in your 'for' loop on line 108.

...
for(int i = 0;i<CountStudents;i++){
    // do work with ClassList[i]...
}

Second, put a check in your 'for' loop to make sure the object in the ClassList[j] position is not null. Simply add a quick check for null.

...
if(ClassList[j] == null) 
    break;  // You can also use continue; if you do want to loop through the entire array.

// do work with ClassList[j]...

I personally suggest using using the second solution as it does not require the counter and it will break out of the loop at the first null position. If there is ever going to be a chance that there are null positions between non-null positions (caused by nulling or deleting a student at a specific position) I would suggest using the 'continue;' statement instead of the 'break;' statement as it will move to the next iteration.

Hope this helps.

-pH

stpatrck 15 Newbie Poster

If you use a 'foreach' or 'for' loop you can simply pass the values from the list. You don't need to create a new Student object in your display method. You also do not need to increment your iterator (i) within the for loop. The 'i++' part of the 'for' loop does this for you. Like ddanbe suggested, I would recommend a 'foreach' loop for your situation...

foreach(var student in _StudentList){
    richTextBox1.Text += "Student: " + student.Name + ...   
}

...but if you must use a simple 'for' loop...

for(int i = 0;i<_StudentList.Length;i++){
    richTextBox1.Text += "Student: " + _StudentList[i].Name + ...
}

You may want to review This MSDN Article about 'for' loops.

Hope this helps. :-)

-pH

stpatrck 15 Newbie Poster

my mistake. the limit statement should be at the end of the query.

Try referring to This StackOverflow Question if it is still not working after moving the limit statement.

ravi142 commented: @Patrick: Thank You +0
stpatrck 15 Newbie Poster

try to order the results by the msg_id descending

something like...
select chat_msgs from user_chat where chat_from_id='2' and chat_to_id='10' limit 0,10 order by msg_id desc