multidimensional array trouble

Why is this not working

It should return a two dimensional array, but I get trouble with the two dimensional array.
OutofMemory exception
outofrange exception
nullreference exeption

In a class I have this method.

public string[][] vulListbox(String query)
{
SQLiteDataReader reader = null;
//if I don't want to initialize here; I have to initialise resultaat also in the catch block// normal what is the normal way to do I want to do in the try block
string[][] resultaat;

int teller = 0;
int velden = 0;
bool gelukt;
try
{
cn.Open();
SQLiteCommand cmd = new SQLiteCommand(query, cn);
reader = cmd.ExecuteReader();
velden = reader.FieldCount;
resultaat = new string[reader.RecordsAffected][];(1)

while (reader.Read())
{
for (int i = 0; i < velden; i++)
resultaat[teller]Idea=reader.GetValue(i).ToString();
here i get nullpointer exception but see (1)

teller++;
}
}
catch (Exception f)
{
//otherwise C# expects me to initialize at
//the beginning
before the try block

resultaat = new string[1][];
MessageBox.Show(f.ToString());
}
finally
{
reader.Close();
reader.Dispose();
cn.Close();
}
return resultaat;
}

In another class I want to call this method

string[][] data = db.vulListbox(querie)

should return a string[][]array, is this the good way of initialisation


If i make the method void and replace, everywhere by for example a string instead of double array and MessageBox.show() the values of the reader everything works fine








Answer this question

multidimensional array trouble

  • Rick112

    resultaat = new string[reader.RecordsAffected][];(1)
    RecordsAffected was null

    while (reader.Read())
    {
    for (int i = 0; i < velden; i++)
    resultaat[teller]Idea=reader.GetValue(i).ToString()
    this had values

    probably is RecordsAffected not meant for select queries
    I put 30 instead



  • Vishal Verma

    //resultaat = new string[reader.RecordsAffected][];(1)

    Now you have an array of null objects.

    for (int i = 0; i < velden; i++)
    resultaat[teller]=reader.GetValue(i).ToString();

    First you need to do this:

    resultaat[teller] = new string[velden];



  • bmkiss67

    hhmmm, difficult, I'll first take a look at multidimensional arrays instead of jagged arrays





  • Jamie Cool

    I think I'll work with a datatable instead of this

  • multidimensional array trouble