locked
'IndexOutOfRangeException was unhandled' ??

    Question

  • Database1DataSet myInstance = new Database1DataSet();

    object Timo = myInstance.Tables["Table1"].Rows[0].ItemArray[0];

    so this is my code (still :p) but it says 'IndexOutOfRangeException was unhandled' but why does it say that error? its not under 0, the number is 0 so is the first row (cant be too high), and what do they mean with: 'Make sure data column names are correct' ? where do I need to type data column names?

    pls help me :<

    thx in advance

    Wednesday, February 07, 2007 3:00 PM

All replies

  • Hi,

    I'm guessing your table has no column, or, more likely, your data table has no rows. In you're code, you're assuming both - that your table has at least one column (that's probably true) and that it contains at least one row (that might not be so). Before executing the above line, test for row (and/or) column presence, like:

    if (myInstance.Tables["Table1"].Rows.Count > 0)
    {
        object Timo = myInstance.Tables["Table1"].Rows[0].ItemArray[0];
    }
    else
    {
       
    // no rows...
    }

    Andrej

    Wednesday, February 07, 2007 4:47 PM
  • ok thx, I have indeed no rows it seems, but was is ment then by rows(I thought rows where your rows in your table, but I have rows in my table)? and where do I add them?
    Wednesday, February 07, 2007 5:34 PM
  •  RubenPieters wrote:
    ...but was is ment then by rows(I thought rows where your rows in your table, but I have rows in my table)?...

    Sorry, didn't quite understand this one...

    About adding rows... you can add rows manually (see ...Rows.Add method), or more common practice - get them from some database.

    Andrej

    Wednesday, February 07, 2007 6:06 PM
  • its easy... what are rows? :x
    Wednesday, February 07, 2007 6:10 PM
  • May i suggest some reading on data concepts in .NET framework? Getting Started with Data Access page on MSDN is a good place to start...

    Andrej

    Wednesday, February 07, 2007 6:51 PM
  •  Andrej Tozon wrote:

    May i suggest some reading on data concepts in .NET framework? Getting Started with Data Access page on MSDN is a good place to start...

    Andrej

    ok i prolly made the question to 'easy' with reading a bit I know now what rows are(they are what I thought they were ^^) but I am not missing any or not not having any at that number, so why does it say i dont have rows?

    Wednesday, February 07, 2007 7:12 PM
  • What exactly are you trying to do? Where are you getting data from?

    Andrej

    Wednesday, February 07, 2007 7:30 PM
  • I'm trying to change data of a specific row, I think I'm getting my data from my dataset :x
    Wednesday, February 07, 2007 7:41 PM
  • I don't see your code, but here's how these things work... You define a dataset, containing some datatables with columns. You have to fill these tables with data, e.g. fill them with data rows". You either do this by reading data from the database (SQL Server, etc.) or by manually adding rows to those tables. Once you have data tables filled with the data, you can start using that data. You can check whether specific tables have data or not by setting a breakpoint on some code line, dealing with datasets, and then hover your mouse cursor over dataset/datatable variable and select the little maginifying lense.

    Again, your code probably tries to access the first row (0) of your Table1. If your table has no rows in it, the row 0 doesn't exist in the data table and you get the IndexOutOfRangeException.

    Andrej

    Wednesday, February 07, 2007 8:58 PM
  • well, I have a database with a table (and with data in it) but somehow it says there aren't rows in it...
    Thursday, February 08, 2007 4:29 PM
  • Ok. so you probably have some code where you're reading that data from the database table into your dataset? Like, using table adapters to fill your dataset (myTableAdapter.Fill(myDataSet.MyDataTable)?

    Can you check if your data gets loaded properly into your local dataset?

    Andrej

    Thursday, February 08, 2007 4:41 PM
  • I have this:

    private void Form1_Load(object sender, EventArgs e)

    {

    this.table1TableAdapter.Fill(this.database1DataSet.Table1);

    }

    Thursday, February 08, 2007 5:18 PM
  • That looks good. Now, try this:

    object Timo = this.database1DataSet.Table1.Rows[0].ItemArray[0];

    It's a variation of your code - it doesn't create a new dataset instance, but uses the table in the same dataset instance - Table1 should now be filled with the code line you posted. The complete code [note: I didn't include row count checking, as specified in one of the previous posts]:

    private void Form1_Load(object sender, EventArgs e)
    {
       
    this.table1TableAdapter.Fill(this
    .database1DataSet.Table1);
        object Timo = this.database1DataSet.Table1.Rows[0].ItemArray[0];
       
    // check Timo here
    }

    Andrej

    Thursday, February 08, 2007 5:53 PM
  • there hasn't changed anything :/
    Thursday, February 08, 2007 6:04 PM
  • Then it looks like your SQL Select statement (used in Fill method) doesn't return any data... One thing... Did you try to check what your dataset instance contains after executing fill? You can invoke Built-in dataset visualizer by hovering over your dataset's name (in your code editor) and select the magnifier lense.

    Andrej

    Thursday, February 08, 2007 6:22 PM
  • "and select the magnifier lense"

    I dont see any magnifier lense, it only shows up a box with: Database1DataSet Form1.database1DataSet

    Thursday, February 08, 2007 7:40 PM
  • There's no icon? How about checking your "Autos" (or "Locals") window in the debugger? You see your dataset instance variable listed?

    Andrej

    Thursday, February 08, 2007 8:07 PM
  • I don't see anything in the locals window :x
    Thursday, February 08, 2007 8:19 PM
  • You are doing this while running your application in debug mode and stopped on a breakpoint in this function (possibly on some line after the Fill method has been executed), aren't you?

    Andrej

    Thursday, February 08, 2007 8:24 PM
  • ow i found the magnifier lense thing I clicked on it and now I get a 'DataSet Visualiser' dialogbox with the data of my table1 ^^

    this is the data(this is just some little application to test things with ;) ):

      1 Tom
      2 Tim
      3 Timmy
      4 Tommy

    Friday, February 09, 2007 6:09 AM
  • OK, the data is there, I still think the following code should get you somewhere:

    private void Form1_Load(object sender, EventArgs e)
    {
        this.table1TableAdapter.Fill(this.database1DataSet.Table1);
        if
    (this.database1DataSet.Table1.Rows.Count > 0)
        {
            object Timo = this.database1DataSet.Table1.Rows[0].ItemArray[0];
        }
    }

    If you step through this code in debugger, does it enter the "if" statement?

    Andrej

    Friday, February 09, 2007 9:36 AM
  • I don't see "if" anywhere :(
    Friday, February 09, 2007 6:50 PM
  • Look at bold, underline part:

    private void Form1_Load(object sender, EventArgs e)
    {
        this.table1TableAdapter.Fill(this.database1DataSet.Table1);
        if
    (this.database1DataSet.Table1.Rows.Count > 0)
        {
            // does your debugger step here?
            object Timo = this.database1DataSet.Table1.Rows[0].ItemArray[0];
        }
    }

    Andrej

    Friday, February 09, 2007 8:25 PM
  • I don't understand what I have to do lol, I added the code but now what?
    Saturday, February 10, 2007 5:55 AM
  • tedIt's called debugging ;)

    In your code window, set a breakpoint at the "if" statement line. You can do that by right-clicking on that line and select Breakpoint | Insert Breakpoint from the contest menu. The line will highlight red and a red dot will appear on the far left side.

    Run your application. The execution should stop at that breakpoint, the line should now be highlighted yellow.

    While focusing your code window, press F10.  The code execution should advance one line. Now, the question is, which line is highlighted yellow now? That's the line which is going to be executed next.

    Andrej

    Saturday, February 10, 2007 2:14 PM
  • private void Form1_Load(object sender, EventArgs e)
    {
        this.table1TableAdapter.Fill(this.database1DataSet.Table1);
        if
    (this.database1DataSet.Table1.Rows.Count > 0)
        {
            object Timo = this.database1DataSet.Table1.Rows[0].ItemArray[0];
        }
    }

    the yellow mark stands at the text that's bald and underlined if I continue to press on F10 it goes to the line under it untill it is at the '}' of Form1_Load, then my apllication starts.

    Saturday, February 10, 2007 4:06 PM
  • ok, so it stops on "object Timo = this---" line? If it does, then I guess all's OK. And if it doesn't, your data adapter's select statement doesn't return any records.

    Andrej

    Saturday, February 10, 2007 8:23 PM
  • But... why does it say I have no rows then? :x
    Sunday, February 11, 2007 7:47 AM
  • Are you still getting the same exception?

    Andrej

    Tuesday, February 13, 2007 10:24 AM