I am importing excelsheet into sqlserver database but there are three columns in excel:
id|data|passport
I want to make sure that all passports start with an alphabet i am getting error at:
if (a[0]>= 'A' && a[0] <= 'Z' && a[0] !='0' )
Error:
Index was outside the bounds of the array.
for (int i1 = 0; i1 < dt7.Rows.Count; i1++)
{
if (dt7.Rows[i1]["passport"]==null)
{
dt7.Rows[i1]["passport"] = 0;
}
string a = Convert.ToString(dt7.Rows[i1]["passport"]);
//char a1 = a[0];
if (a[0]>= 'A' && a[0] <= 'Z' && a[0] !='0' )
{
Label12.Text = "CAPITAL";
break;
}
else
{
Label12.Text = "notgood";
flag = flag + 1;
}
It sounds like a
references an empty array, which therefore doesn't have an element at index 0
. You'll need to check that the array isn't empty before trying to access the first element.
It certainly used to be the case that objects in word and excel ranges were 1-based arrays and would throw an out of range exception when index 0 was accessed. I believe this is still the case.
It is also possible, as @anthony-grist said, that the array is empty, and therefor the first item will be outside of the array bounds.
I recommend that you test the length of the array, and start access at index 1.
Why not just modify your condition like this
If(!String.IsNullOrEmpty(a)){
If(Char.IsLetter(a[0])){
Label12.Text = "CAPITAL";
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.