0

I have a stored procedure that does this:

SELECT TOP 4 FROM dbo.test

(table contains 5 rows)

My c# code is:

IDataReader test= ((IDataReader)(DataProvider.Instance().ExecuteReader("fetchtest")));
 test.Read();
 title1.Text = test.GetString(0);
 title2.Text = test.GetString(1);
 title3.Text = test.GetString(2);
 title4.Text = test.GetString(3); 

However I can only display 0 and 1. 2+ will give me an index error. Is there a better way to retrieve all rows?

Thanks

tdjfdjdj
  • 2,391
  • 13
  • 44
  • 71

2 Answers2

2

IDataReader.GetString(2) returns the value of the third column, not record. You need to use Read() method to advance to the next record.

a1ex07
  • 36,826
  • 12
  • 90
  • 103
0

Like a1ex07 said, GetString gets the nth column. Here is a complete code sample.

List<string> titles = new List<string>();
using (IDataReader test = ((IDataReader)(DataProvider.Instance().ExecuteReader("fetchtest"))))
{
    while (test.Read())
    {
       titles.Add(test.GetString(0));
    }
}


title1.Text = titles[0];
title2.Text = titles[1];
title3.Text = titles[2];
title4.Text = titles[3];
Fammy
  • 513
  • 3
  • 14