![](/img/trans.png)
[英]Index was outside on the bounds of array error on c# log in with SQL
[英]C# & SQL Server : Index was outside the bounds of the array
我正在尝试从SQL Server数据库获取数据,但是该错误状态
System.IndexOutOfRangeException:索引超出数组的范围。
这是我的桌子
我的代码:
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;
}
}
如您所见,代码应该没问题,因为表中有3个数据,但是GetString(1)
却有问题。
似乎在select语句中,您正在选择ONE列,但在阅读器中,您正在尝试选择两个。
示例: SELECT Id, BicycleType FROM TourDisplay ORDER BY TDateTime
请在您的选择语句中添加另一列,或从阅读器中删除第二个选择器:
TBBicycleType2.Text = dtr.GetString(1);
按评论编辑
看来您可能正在尝试设置两个不同的按钮,并使用此while读者循环使用查询的前两个结果。 我将对其进行如下调整:
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;
}
}
虽然,如果您只是使用while循环并从数据库中设置两个按钮,我会重新考虑一下您的设计吗?
对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++;
}
}
索引确实超出范围。 由于您尝试获取dtr.getString(1)的第二列。 要修复,请删除dtr.getString(1)。
在每次迭代中,dtr.GetString(0)获取BicycleType的下一行。 因此,第一次迭代TBBicycleType.Text =“ Time Trial”。 第二次迭代,TBBicycleType.Text =“ MTB”。 等等。
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.