system.data.dll中发生了'system.indexoutofrangeexception'类型的未处理异常 [英] An unhandled exception of type 'system.indexoutofrangeexception' occurred in system.data.dll
问题描述
我无法从excel中获取第7列值,因为System.Data.dll中发生了'System.IndexOutOfRangeException'类型的未处理异常
和其他信息:找不到第7栏。
但是iam在第7列的Excel中有价值,即使我也不能能够在第7个值之后获取。我附上了我的excelsheet和编码。
请做必要的。
I could not able to fetch 7th column value from excel it throws an error as "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll"
and Additional information: Cannot find column 7.
But iam having values in excel for 7th column, Even I couldn't able to fetch after 7th value. I have attached my excelsheet and coding.
Kindly do the needful.
int j=0;
private void ImportExcel(string strFilePath)
{
if (File.Exists(strFilePath))
{
String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=" + strFilePath + ";"
+ "Extended Properties='Excel 8.0;HDR=Yes'";
connExcel = new OleDbConnection(strExcelConn);
try
{
cmdExcel.Connection = connExcel;
//Check if the Sheet Exists
connExcel.Open();
DataTable dtExcelSchema;
//Get the Schema of the WorkBook
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connExcel.Close();
//Read Data from Sheet1
connExcel.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
da.SelectCommand = cmdExcel;
da.Fill(ds);
DisplayText();//Method
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ImportExcel");
}
finally
{
cmdExcel.Dispose();
connExcel.Dispose();
}
}
private void DisplayText()
{
BAngle();
BFAngle();
}
private void BAngle()
{
int Angle = 0;
for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
{
string sname = ds.Tables[0].Rows[i][Angle].ToString();
txt_BSA.Text = sname.ToString();
txt_BSA.Focus();
}
}
private void BFAngle()
{
int Fore = 7;
for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
{
string bname = ds.Tables[0].Rows[i][Fore].ToString();//It throws an error
txt_BFA.Text = bname.ToString();
txt_BFA.Focus();
}
}
我已将excel表附在下面的链接中。
Excel.xls - Google云端硬盘 [ ^ ]
我尝试过:
private void BFAngle()
{
int Fore = 7;
for(int i = j; i< = ds.Tables [0] .Rows.Count; i ++)
{
string bname = ds.Tables [0] .Rows [i] [Fore] .ToString();
txt_BFA.Text = bname.ToString();
txt_BFA.Focus();
}
I have attached my excel Sheet in below link.
Excel.xls - Google Drive[^]
What I have tried:
private void BFAngle()
{
int Fore = 7;
for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
{
string bname = ds.Tables[0].Rows[i][Fore].ToString();
txt_BFA.Text = bname.ToString();
txt_BFA.Focus();
}
推荐答案
尝试替换
Try to replace
for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
with
with
for (int i = j; i < ds.Tables[0].Rows.Count; i++)
基于0的列表,最后一个元素是Count-1
[更新]
测试是否
With a 0 based list, the last element is Count-1
[Update]
Test if
ds.Tables[0].Rows[i].Count
至少为8,因为您尝试读取元素7.
[更新]
is at lest 8 because you try to read element 7.
[Update]
编译器显示错误为错误1运算符< ='不能应用于操作数类型'int'和'方法组'
Compiler shows error as "Error 1 Operator '<=' cannot be applied to operands of type 'int' and 'method group'"
看起来你尝试访问 ds.Tables [0] .Rows
作为2D数组实际上是一维数组。
ds.Tables [0] .Rows [i] [Fore]
不存在因为
ds.Tables [0] .Rows [i]
是一个int和
d s.Tables [0] .Rows
是一个数组
Looks like you try to access ds.Tables[0].Rows
as a 2D array when it is in fact a 1D array.
ds.Tables[0].Rows[i][Fore]
do not exist because
ds.Tables[0].Rows[i]
is an int and
ds.Tables[0].Rows
is an array
这篇关于system.data.dll中发生了'system.indexoutofrangeexception'类型的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!