How to copy values from reader into 2 dim array ? RRS feed

  • Question

  • Hi all,

    I am executing following code




    [,] schoolarray = new string[scount, scount];




    (var cmd = db.CreateCommand())


    cmd.CommandText =


    "Select studentid, degreeid..."; //For one student multiple degree eg 1-0,1-1, 1-2, 2-0...



    using (var reader = cmd.ExecuteReader())




    while (reader.Read())


    schoolarray[j,] = reader[



    schoolarray[,k] = reader["degreeid"].ToString();






    Please help me in correcting my mistake in assigning the reader values in multidimentional array.


    Monday, June 7, 2010 10:36 PM


  • The thing to notice here is that studentid will always go in the second-dimension subscript 0 (the first column of the matrix) and the degreeid will always go in the second-dimension subscript 1 (the second column of the matrix).  The first subscript (the row number) will start at zero (the first row of the matrix) and increase by one at the end of the while body on each iteration.

    int studentIndex = 0;
    while (reader.Read())
      schoolarray[studentIndex, 0] = reader["studentid"].ToString(); 
      schoolarray[studentIndex, 1] = reader["degreeid"].ToString();



    Tuesday, June 8, 2010 12:49 AM