Reputation: 46
I know my question seems very generic and basic but I can't seem to find the problem. When VS starts to debug my code at the declaration of the integer value "i" this error pops up:
So I have tried to declare before the loop but same error remain. After that I have changed the type of variable, yet no change. But when I declared it between the "masaQuery = ..." part, it started throw error at the assigment part of the for loop. So I've changed it that I can trick it but still the same result:
First Version Of the code :
string[] masaQuery = new string[8];
masaQuery[0] = uyelik;
masaQuery[1] = ucretler;
masaQuery[2] = masalar;
masaQuery[3] = masaHareket;
masaQuery[4] = log;
masaQuery[5] = genelAyarlar;
masaQuery[6] = adisyon;
masaQuery[7] = direktAdisyon;
masaQuery[8] = bekleyenListesi;
masaQuery[9] = borclular;
for (int i = 0; i < masaQuery.Length; i++)
{
using (SQLiteConnection c = new SQLiteConnection(connStr))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(masaQuery[i], c))
{
cmd.ExecuteNonQuery();
}
}
}
Last Version of the code :
string[] masaQuery = new string[9];
masaQuery[0] = uyelik;
masaQuery[1] = ucretler;
masaQuery[2] = masalar;
masaQuery[3] = masaHareket;
masaQuery[4] = log;
int i = 0;
masaQuery[5] = genelAyarlar;
masaQuery[6] = adisyon;
masaQuery[7] = direktAdisyon;
masaQuery[8] = bekleyenListesi;
masaQuery[9] = borclular;
for (; i < masaQuery.Length; i++)
{
var query = masaQuery[i];
using (SQLiteConnection c = new SQLiteConnection(connStr))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(query, c))
{
cmd.ExecuteNonQuery();
}
}
}
Waiting for your answers.
Upvotes: 0
Views: 799
Reputation: 24957
From array documentation in MSDN:
- Arrays are zero indexed: an array with
n
elements is indexed from0
ton-1
.
When you declare an array with n
= 9, the array index actually assigned from 0
to 8
, and when you're using 9th index will throw IndexOutOfBoundsException
because that index does not exist.
Instead of using n
= 9, use n
= 10 instead:
string[] masaQuery = new string[10];
Upvotes: 1
Reputation: 181
This should do the trick:
string[] masaQuery = new string[10];
You have 10 elements but you're only creating a string array that can hold 9 elements.
Upvotes: 3