Hi again!

I have a database and some tables filled with data.

var club = Baza.Clubs; //Baza is dataset, and Clubs is table
            var schedule = Baza.Schedule; // same as above

            for (int i = 1; i <= 90; i++)
            {
                if(schedule[i].day == 2) // BOOM! Breaks at first pass..
                {
                    //Do something..
                }
            }

When I run my app it throws me this error: There is no row at position 0
and throws me to to this part of code:

public ScheduleRow this[int index] {
                get {
                    return ((ScheduleRow)(this.Rows[index]));
                }
            }

Any ideas why this happens?

Dani AI

Generated

Short summary: that error means your code tried to read a row by numeric index from a typed DataTable when the table had no rows (the typed indexer delegates to the DataRowCollection and throws when Rows.Count == 0). In this thread was right to suggest a null/exists check and was right to suggest inspecting Locals — Locals showing columns but zero rows is the key clue. fixed it by ensuring the table is populated before iterating and by iterating with 0-based bounds.

Quick checklist to avoid it:

  • Confirm the DataTable actually contains rows (check Rows.Count or the generated Count property) before indexing.
  • Make sure the TableAdapter/DataAdapter.Fill ran successfully (right connection string, query, parameters) before you read rows.
  • Iterate 0-based: start at 0 and use < Count (or better, use foreach). Avoid hard-coded upper bounds.
  • Ensure you are inspecting the same DataSet instance at runtime that your code is using (designer/runtime instances can be different).
  • Use defensive checks for DBNull/IsNull on columns before casting.

Example safe pattern (generic names to match your codebase):

if (myTable != null && myTable.Rows.Count > 0) {
    foreach (DataRow row in myTable.Rows) {
        if (!row.IsNull("day") && Convert.ToInt32(row["day"]) == 2) {
            // handle matching row
        }
    }
}

Extra tips: when a table suddenly shows zero rows after deleting/re-adding a dataset, re-check the TableAdapter configuration and the dataset instance your form uses. Prefer foreach or LINQ (AsEnumerable/Field<T>) over manual index arithmetic; it avoids off-by-one errors and is more robust during maintenance.

Recommended Answers

All 8 Replies

Is Schedule null when it is assigned?

Is Schedule null when it is assigned?

Sorry, not sure what you mean.. Where is it assigned? At 'var schedule = Baza.schedule'?

And btw, Baza.Clubs has 10 rows according to Locals, but Baza.Schedule does not have any..

var schedule = Baza.Schedule; // same as above
if(null == schedule)
{
   //Something is wrong before it got to here
}
var schedule = Baza.Schedule; // same as above
if(null == schedule)
{
   //Something is wrong before it got to here
}

No, thats okay..

Surely there is just a lack of rows in whatever you are pulling as you are looking for the row (2) so the third row in. Run a debug and use a break point to see what is contained within the schedule and see if that helps to solve the issue?

Im making assumptions but the error as Thines01 said could either be that it is null or that the schedule actually contains no rows.

Surely there is just a lack of rows in whatever you are pulling as you are looking for the row (2) so the third row in. Run a debug and use a break point to see what is contained within the schedule and see if that helps to solve the issue?

Im making assumptions but the error as Thines01 said could either be that it is null or that the schedule actually contains no rows.

I've been through locals, and it contains only columns, but no rows. I'm not sure why. Why do the other tables work properly?

Kind of impossible to tell from the information provided.

Common sense would say either the Baza.Schedule object doesnt contain anything row wise or there is a failing link in your database working somewhere preventing the rows being pulled?

Maybe give us a more extensive code post and an insight into the type of informaation that you should be expecting?

I managed to get it to work. The thing I changed was this:

var club = Baza.Clubs; //Baza is dataset, and Clubs is table
    var schedule = Baza.Schedule; // same as above
     
    for (int i = 0; i < schedule.count; i++) // THIS WAS REPLACED
    {
    if(schedule[i].day == 2)
    {
    //Do something..
    }
    }

AND MOST IMPORTANTLY... I added this in form load:

scheduleTableAdapter.Fill(Baza.schedule);

I had some database issues, so I had to delete it, and add again, and thats why this error was created. Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.