I see a few errors in this statement. First, as OriginalGriff pointed out, your
For
loop has an "index out of bounds" error. Next, you are creating a new
SqlCommand
for each item in your
For
loop, effectively overwriting the previous entries. You need to put the call to the database inside your
For
loop. Finally, you are using the ExecuteReader command when making an
INSERT
call. You should instead be using the
ExecuteNonQuery[
^] method, which is designed for
INSERT
statements.
Once you get through all of that, I think you need to look at your code design as well. There are a few issues that I think you should address. First, you use letters as variable names. This is not descriptive and it can lead to confusion later on, which can lead to the introduction of bugs into your application. Next, you are doing one insert per row. I'm not sure how big your list is, but that can be anything from inefficient to catastrophic. Instead, look at doing one
INSERT
statement that puts all of the values in at once. The actual SQL code would end up looking like this:
INSERT INTO Table(column)
SELECT ListItemOne
UNION ALL
SELECT ListItemTwo
UNION ALL
etc.
You would need to modify your loop to build a SQL statement like this, but the end result is one call to the database instead of multiple.