简体   繁体   English

C#和SQL Server:索引超出了数组的范围

[英]C# & SQL Server : Index was outside the bounds of the array

I'm trying to get data from the SQL Server database but the error state that 我正在尝试从SQL Server数据库获取数据,但是该错误状态

System.IndexOutOfRangeException: Index was outside the bounds of the array. System.IndexOutOfRangeException:索引超出数组的范围。

This is my table 这是我的桌子

TourDisplay 巡回展示

My code: 我的代码:

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=....;Integrated Security=True;Connect Timeout=30;User Instance=True;");

SqlCommand cmd = new SqlCommand();
SqlDataReader dtr;

protected void Page_Load(object sender, EventArgs e)
{
    cmd.Connection = conn;
    conn.Open();

    cmd.CommandText = "select BicycleType from TourDisplay order by TDateTime asc";

    dtr = cmd.ExecuteReader();
    bool temp = false;

    while (dtr.Read())
    {
        TBBicycleType.Text = dtr.GetString(0);
        TBBicycleType2.Text = dtr.GetString(1);
        temp = true;
    }     
}

As you can see, the code should be fine because I got 3 data inside the table but I'm having problem on GetString(1) . 如您所见,代码应该没问题,因为表中有3个数据,但是GetString(1)却有问题。

It appears that in your select statement, you are selecting ONE column, but in the reader, you're trying to select two. 似乎在select语句中,您正在选择ONE列,但在阅读器中,您正在尝试选择两个。

EXAMPLE: SELECT Id, BicycleType FROM TourDisplay ORDER BY TDateTime 示例: 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. 看来您可能正在尝试设置两个不同的按钮,并使用此while读者循环使用查询的前两个结果。 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? 虽然,如果您只是使用while循环并从数据库中设置两个按钮,我会重新考虑一下您的设计吗?

Edit 2 per changes to the OP mentioning 3 total rows in the database 对OP的每个更改进行2编辑 ,提及数据库中的3行

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++;
    }     
}

The index is indeed out of bounds. 索引确实超出范围。 Since you're trying to get the second column on dtr.getString(1). 由于您尝试获取dtr.getString(1)的第二列。 To fix, remove the dtr.getString(1). 要修复,请删除dtr.getString(1)。

In each iteration, dtr.GetString(0) gets the next row of BicycleType. 在每次迭代中,dtr.GetString(0)获取BicycleType的下一行。 So first iteration, TBBicycleType.Text = "Time Trial". 因此,第一次迭代TBBicycleType.Text =“ Time Trial”。 Second iteration, TBBicycleType.Text = "MTB". 第二次迭代,TBBicycleType.Text =“ MTB”。 and so on. 等等。

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=....;Integrated Security=True;Connect Timeout=30;User Instance=True;");
SqlCommand cmd = new SqlCommand();
SqlDataReader dtr;


protected void Page_Load(object sender, EventArgs e)
{
    cmd.Connection = conn;
    conn.Open();
    cmd.CommandText = "select BicycleType from TourDisplay order by TDateTime asc";
    dtr = cmd.ExecuteReader();
    bool temp = false;
    while (dtr.Read())
    {
        TBBicycleType.Text = dtr.GetString(0);
        temp = true;
    }     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM