tdjfdjdj
tdjfdjdj

Reputation: 2471

sql stored procedure - Having problems retreiving multiple rows of data with Idatareader

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

Upvotes: 0

Views: 580

Answers (2)

Fammy
Fammy

Reputation: 563

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];

Upvotes: 0

a1ex07
a1ex07

Reputation: 37364

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

Upvotes: 2

Related Questions