It appears that in your select statement, you are selecting ONE column, but in the reader, you're trying to select two.
EXAMPLE: SELECT Id, BicycleType FROM TourDisplay ORDER BY TDateTime
Please either add another column to your select statement, or remove the second selector from the reader:
TBBicycleType2.Text = dtr.GetString(1);
Edit per comment
It appears that you may be trying to set two different buttons, using the first two results from your query with this while reader loop. I'd tweak it a little as follows:
protected void Page_Load(object sender, EventArgs e)
{
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "SELECT BicycleType FROM TourDisplay ORDER BY TDateTime";
dtr = cmd.ExecuteReader();
bool temp = false;
// Added this variable. name it anything you like.
bool isFirstItem = true;
while (dtr.Read())
{
// After the first loop iteration, isFirstItem will be set to false
if (isFirstItem)
{
TBBicycleType.Text = dtr.GetString(0);
isFirstItem = false;
}
// All consequent iterations of the loop will set the second
// item's 'Text' property. So if you have only two rows, it
// will be the second row. If you have 20 rows, it will be the 20th row.
else
TBBicycleType2.Text = dtr.GetString(0);
temp = true;
}
}
Although, if you're just setting two buttons using a while loop and from the database, I'd rethink your design a little?
Edit 2 per changes to the OP mentioning 3 total rows in the database
protected void Page_Load(object sender, EventArgs e)
{
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "SELECT BicycleType FROM TourDisplay ORDER BY TDateTime";
dtr = cmd.ExecuteReader();
// Again, I'd rethink your design, but if you're just playing
// around this should be fine
int count = 0;
while (dtr.Read())
{
switch (count)
{
case 0:
TBBicycleType.Text = dtr.GetString(0);
break;
case 1:
TBBicycleType2.Text = dtr.GetString(0);
break;
// Add whatever you want to do with the third one here
// case 2:
// TBBicycleType3.Text = dtr.GetString(0);
// break;
}
count++;
}
}
dtr. Then it would be easy for us to find the issueBicycleTypebut trying to fetch two from the read array. Either select more columns in your sql statement, or remove the second GetStringBicycleTypeand you are trying to access the second column usingGetString()which is not yet fetched. Your issue will be fixed if you add the second column name in the query